问题描述
我已经阅读了很多以前关于数组的问题,但是我只是无法使它起作用:(我的数组看起来像:-
I've read plenty of previously asked questions about arrays but I'm just not able to get this to work :( My array looks like:-
Array
(
[1259] => Array
(
[status] => 0
[cond] => 0
)
[1461] => Array
(
[status] => 0
[cond] => 0
)
)
键根据用户输入而有所不同。当前尝试做的是遍历输入,并在其父数组键旁边回显数组内容。
The keys vary depending on user input. All I'm currently trying to do is loop through the input and echo out the array contents alongside its parent arrays key.
我的代码如下:-
foreach($games as $key => $value) {
echo 'key: '.$key.' - ';
foreach( $value as $game){
echo $game["status"].' - '.$game["cond"].'<BR>';
}
}
这是不正确的,因为它会回显状态并为每个数组项进行两次cond。还读到它是不正确的ct可以在另一个foreach中使用一个foreach。
This is incorrect because it is echoing the status and cond twice for each array item. I've also read that it is incorrect to use a foreach inside another foreach.
下面的代码几乎在那里,但是我在回显状态键和cond信息时遇到了麻烦:-
The following code is almost there but I'm having trouble echoing the array key with the status and cond information:-
foreach( $games as $game){
echo $game["status"].' - '.$game["cond"].'<BR>';
}
是否可以在上面回显数组键(例如1259)代码?
Is it possible to echo the array key (eg. 1259) in the above code?
非常感谢
推荐答案
尝试:
foreach( $games as $key => $game){
echo 'Key: '.$key.' - '.$game["status"].' - '.$game["cond"].'<BR>';
}
这篇关于数组内的PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!