php嵌套数组到字典

时间:2013-10-29 22:36:32

标签: php arrays object dictionary

我怎样才能得到这个:

(
[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
    )

看起来像这样:

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

我试过了:

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

我认为这应该可行,但它会让我看起来完全一样。这只是从jquery传递到POST的serializedArray();我基本上只是希望能够使用$ fields ['variation']访问来访问它。但它让它变得困难。

1 个答案:

答案 0 :(得分:3)

您没有正确使用嵌套数组,$value使用namevalue保存数组,因为$key(0,1,...)是不需要,你甚至不需要定义它。

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