Gisheri
Gisheri

Reputation: 1715

php nested array to dictionary

How can I get this:

(
[0] => Array
    (
        [name] => variation
        [value] => variation1
    )

[1] => Array
    (
        [name] => variationid
        [value] => 70105
    )

[2] => Array
    (
        [name] => fullName
        [value] => 
    )

[3] => Array
    (
        [name] => address
        [value] => 
    )

[4] => Array
    (
        [name] => country
        [value] => usa
    )

[5] => Array
    (
        [name] => state
        [value] => Utah
    )

to look like this:

$fields['variation']=>variation1[variationid]=>70105.. etc

I tried:

foreach($_POST['fields'] as $key => $value){
    $fields[$key] = $value;
}

I thought this should work, but it gives it back to me looking the exact same way. This is just a serializedArray() from jquery passed into POST; I basically just want to be able to access it using the $fields['variation'] accessing. But it's making it difficult.

Upvotes: 1

Views: 3059

Answers (1)

kero
kero

Reputation: 10638

You aren't using the nested array correctly, $value holds the array with name and value and since $key (0,1,...) is not needed, you don't even need to define it.

foreach ($_POST['fields'] as $data) {
    $fields[ $data['name'] ] = $data['value'];
}

Upvotes: 3

Related Questions