我有一个问题,我需要通过一个数组与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 :(得分: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
答案 1 :(得分: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++;
}
这应该是你的答案..
答案 2 :(得分:0)
这应该有所帮助:
$a_data['syn_label'][] = urldecode(utf8_decode($a_data['syn_label']));
对于每次迭代,您只能替换$a_data['syn_label']
。通过添加[]
,您将使其成为一个多维数组,每次迭代都会递增。