2015-04-19 145 views
-1

我有一個多維數組,它是從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

如何從'Contact'得到'聯繫name'?你嘗試過什麼嗎? – Rizier123

+0

在您的'$ _POST'上使用foreach,並將'['name']'設置爲鍵,將[['value']'設置爲新數組中的值。 – Sean

+0

我做的foreach($ _POST [ '數據']爲$ⅰ){ \t \t的foreach($ i設定爲$密鑰=> $值){ \t \t \t //滯留在這裏 \t \t} \t – Paudel

回答

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);