Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] insert defaut values into array
#1

[eluser]Dagobert Renouf[/eluser]
Hello everyone,

I'm currently using config files for my application, and I set them as array.

- My defaut config file with all fields possible and associated values
- The user config file with some fields associated with their values

I need to create, from theses two arrays, an array with the user values in it AND if some field is not specified, then insert the default value.

Code:
//array config default

            [label] => ""
            [type] => input
            [rules] => ""
            [prep] => ""
            [display] => 1
            [thumbs] => ""

            //array config user
            
            [label] => "email"
            [type] => input
            [rules] => "required"

            // array I need

            [label] => "email"
            [type] => input
            [rules] => "required"
            [prep] => ""
            [display] => 1
            [thumbs] => ""
#2

[eluser]xwero[/eluser]
Code:
array_merge($default,$user)
#3

[eluser]Dagobert Renouf[/eluser]
thanks a bunch for this.
#4

[eluser]Dagobert Renouf[/eluser]
Now I need to make my array cleaner, how can I go from this :

Code:
Array
(
    [title] => Array
        (
            [label] => Date
            [type] => hidden_last
            [rules] =>
            [prep] =>
            [display] => 1
            [thumbs] =>
            [value] => date
        )

    [body] => Array
        (
            [label] => Title
            [type] => input
            [rules] => required
            [prep] => htmlentities
            [display] => 1
            [thumbs] =>
        )

)

to this :

Code:
Array
(
    Array
        (
            [name] => title
            [label] => Date
            [type] => hidden_last
            [rules] =>
            [prep] =>
            [display] => 1
            [thumbs] =>
            [value] => date
        )

    Array
        (
            [name] => Body
            [label] => Title
            [type] => input
            [rules] => required
            [prep] => htmlentities
            [display] => 1
            [thumbs] =>
        )

)

Any thoughts ?
#5

[eluser]xwero[/eluser]
Why would you want to do that? If you loop the array you don't have the know which key the 'row' has and even if you need to use the key you can do
Code:
foreach($array as $key=>$subarray)
{
   $array[$key] = $subarray;
}
So you don't need to know the key, php figures it out for you Smile
#6

[eluser]Dagobert Renouf[/eluser]
I'm creating a library right now and I want it to be as clean as possible, so having just a two dimensionnal array is much more clearer for me.

I don't get what you propose here, could you explain how this would work ? (I tried and it doesn't).
#7

[eluser]xwero[/eluser]
The only difference between the two arrays is that the first has a words as keys and the second has numbers as keys. You can see this if you use var_dump.

What i was demonstrating was that you don't have to know if the key is a word or a number in a foreach loop because it can identify the key for you. The example changes nothing because it puts the $subarray back in its place.

So if you are going to manipulate the array you are only going to slow down the rendering of your page. But after saying that here is a way to do it
Code:
$array = array_combine(range(0, count($array)-1),$array);
#8

[eluser]Dagobert Renouf[/eluser]
thanks a lot for the explanation xwero, I'll do it the way you advise Wink
#9

[eluser]Seppo[/eluser]
I noticed that the key is added as an array item with the key "name"
So, to transform the array I would...
Code:
foreach ($array as $key => $value)
{
$array[$key]['name'] = $key;
}

$array = array_values($array);
#10

[eluser]xwero[/eluser]
I don't know what you are trying to do with that foreach but array_values is certainly a better solution than my array_combine solution.




Theme © iAndrew 2016 - Forum software by © MyBB