将多维数组的值更改为新数组

时间:2015-04-19 20:53:19

标签: php jquery arrays multidimensional-array

我有一个多维数组,它是从jquery' .serializeArray()方法发布的,我通过$_POST全局变量得到了这个数组。

(
[0] => Array
    (
        [name] => Name
        [value] => Posted Name
    )

[1] => Array
    (
        [name] => Contact
        [value] => Posted Number
    )

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

我需要一个看起来像这样的数组:

Array (
    'Name' => 'Posted Name',
    'Contact name' => 'Posted Number',
    'email' => ''
)

1 个答案:

答案 0 :(得分:0)

$postArray[0] = array('name' => 'Name', 'value' => 'Posted Name');
$postArray[1] = array('name' => 'Contact', 'value' => 'Posted Number');
$postArray[2] = array('name' => 'email', 'value' => '');

foreach($postArray as $item) {

    if($item['name'] == "Name")
        $newArr['name'] = $item['value'];

    if($item['name'] == "Contact")
        $newArr['Contact name'] = $item['value'];

    if($item['name'] == "email")
        $newArr['email'] = $item['value'];

}

print_r($newArr);