2014-09-02 67 views
0

我有一個問題要問你,我需要通過在PHP中其他數組的數組,但我只通過最後一個數組,我的數組是:通過在PHP與其他數組的數組

Array 
(
[0] => Array 
    (
     [syn_id] => 17070 
     [syn_label] => fd+dfd 
    ) 
[1] => Array 
    (
     [syn_id] => 17068 
     [syn_label] => fds+dsfds 
    ) 
[2] => Array 
    (
     [syn_id] => 17069 
     [syn_label] => klk+stw 
    ) 
) 

我的PHP :?

 $a_ddata = json_decode(method(), true); 
     foreach ($a_ddata as $a_data) 
     { 
      $a_data['syn_label'] = urldecode(utf8_decode($a_data['syn_label'])); 
     } 

有了這個代碼,我只通過最後一個數組[2],但如何通過陣列請幫我 我需要得到數組:

Array 
(
[0] => Array 
    (
     [syn_id] => 17070 
     [syn_label] => fd dfd 
    ) 
[1] => Array 
    (
     [syn_id] => 17068 
     [syn_label] => fds dsfds 
    ) 
[2] => Array 
    (
     [syn_id] => 17069 
     [syn_label] => klk stw 
    ) 
) 
+0

顯示我們預期的結果。 – 2014-09-02 12:48:37

+2

每次迭代都會覆蓋您的值。 – Daan 2014-09-02 12:48:46

+0

我編輯了問題 – 2014-09-02 12:50:08

回答

1
 $a_ddata = json_decode(method(), true); $i=0; 
     foreach ($a_ddata as $a_data) 
     { 
      $a_data_f[$i]['syn_id'] = $a_data['syn_id']; 
      $a_data_f[$i]['syn_label'] = urldecode(utf8_decode($a_data['syn_label'])); 
      $i++; 
     } 

這應該是你的答案..

+1

Thnx Shaunak .... – 2014-09-02 13:15:16

1

當您使用foreach來遍歷某些東西時,默認情況下PHP會爲每個元素創建一個副本,供您在循環中使用。因此,在你的代碼,

$a_ddata = json_decode(method(), true); 
foreach ($a_ddata as $a_data) 
{ 
    // $a_data is a separate copy of one of the child arrays in $a_ddata 
    // this next line will modify the copy 
    $a_data['syn_label'] = urldecode(utf8_decode($a_data['syn_label'])); 
    // but at the end of the loop the copy is discarded and replaced with a new one 
} 

幸運的是manual page for foreach爲我們提供了一種方式來超越基準運營商&此行爲。如果將它放在as關鍵字和循環變量之間,則可以更新循環中的源數組。

$a_ddata = json_decode(method(), true); 
foreach ($a_ddata as &$a_data) 
{ 
    // $a_data is now a reference to one of the elements to $a_ddata 
    // so, this next line will update $a_ddata's individual records 
    $a_data['syn_label'] = urldecode(utf8_decode($a_data['syn_label'])); 
} 
// and you should now have the result you want in $a_ddata 
0

這應有助於:

$a_data['syn_label'][] = urldecode(utf8_decode($a_data['syn_label'])); 

對於每次迭代,你只能更換$a_data['syn_label']。通過添加[],您將使其成爲一個多維數組,每次迭代都會遞增。