好吧所以我试图找出如何将数据添加到关联数组中。该数组看起来像:
Array
(
[games] => Array
[game] => Array
[0] => Array
[appID] => 12345
[logo] => 'http://www.example.com/logo_12345.jpg'
[name] => 'Game One'
[1] => Array
[appID] => 12346
[logo] => 'http://www.example.com/logo_12346.jpg'
[name] => 'Game Two'
[2] => Array
[appID] => 12347
[logo] => 'http://www.example.com/logo_12347.jpg'
[name] => 'Game Three'
[userID] => 784523569852145
[username] => 'Gamertag'
)
我想要做的是循环数组并添加以下元素,使它看起来像这样:
Array
(
[games] => Array
[game] => Array
[0] => Array
[appID] => 12345
[logo] => 'http://www.example.com/logo_12345.jpg'
[name] => 'Game One'
[status] => 'active'
[1] => Array
[appID] => 12346
[logo] => 'http://www.example.com/logo_12346.jpg'
[name] => 'Game Two'
[status] => 'active'
[2] => Array
[appID] => 12347
[logo] => 'http://www.example.com/logo_12347.jpg'
[name] => 'Game Three'
[status] => 'active'
[userID] => 784523569852145
[username] => 'Gamertag'
)
答案 0 :(得分:4)
它会是这样的:
foreach($my_array["games"]["game"] as &$game) {
$game["status"] = "active";
}
&
至关重要,因为没有它,变量$game
将引用相关数组的副本,而不是数组本身。换句话说,您的更改不会被保存。
答案 1 :(得分:2)
$iamlazy =& the_array['games']['game'];
$n=sizeof($iamlazy);
for ($i=0;$i<$n;%i++)
$iamlazy[$i]['status']=active;
答案 2 :(得分:0)
您可以在需要的地方添加新元素,只需使用方括号来标识要添加的元素,并按照正常情况分配:
$array['games']['game'][0]['status']='Active';
这不是一个循环,但我很确定你可以从这里开始根据你的结构制作循环。