I am trying to loop through an array with duplicate indexes. But it only prints 3 times not all of them. I want to print all values in the array is that possible?
我试图循环使用重复索引的数组。但它只打印3次而不是全部。我想打印数组中的所有值是否可能?
Here is my PHP code:
这是我的PHP代码:
$data['Video'][0]['name']='a';
$data['Video'][1]['name']='b';
$data['Video'][1]['name']='c';
$data['Video'][3]['name']='d';
$data['Video'][3]['name']='e';
foreach ($data['Video'] as $video) {
print_r($video);
}
And here is the output of that code:
以下是该代码的输出:
Array
(
[name] => a
)
Array
(
[name] => c
)
Array
(
[name] => e
)
2 个解决方案
#1
0
Please avoid duplicate entry of keys otherwise try this,
请避免重复输入密钥,否则请尝试此操作,
$data['Video'][]['name']='a';
$data['Video'][]['name']='b';
$data['Video'][]['name']='c';
$data['Video'][]['name']='d';
$data['Video'][]['name']='e';
foreach ($data['Video'] as $video) {
print_r($video);
}
#2
1
Well, the duplicate indexes negate each other. So this is expected behavior. So when you set this in your code:
好吧,重复的索引相互否定。所以这是预期的行为。所以当你在代码中设置它时:
$data['Video'][0]['name']='a';
$data['Video'][1]['name']='b';
$data['Video'][1]['name']='c';
$data['Video'][3]['name']='d';
$data['Video'][3]['name']='e';
It really just means this:
它真的只是意味着:
$data['Video'][0]['name']='a';
$data['Video'][1]['name']='c';
$data['Video'][3]['name']='e';
The newer data assigned to the keys 1
and 3
overwrite what was previously there.
分配给键1和3的较新数据会覆盖之前的数据。
#1
0
Please avoid duplicate entry of keys otherwise try this,
请避免重复输入密钥,否则请尝试此操作,
$data['Video'][]['name']='a';
$data['Video'][]['name']='b';
$data['Video'][]['name']='c';
$data['Video'][]['name']='d';
$data['Video'][]['name']='e';
foreach ($data['Video'] as $video) {
print_r($video);
}
#2
1
Well, the duplicate indexes negate each other. So this is expected behavior. So when you set this in your code:
好吧,重复的索引相互否定。所以这是预期的行为。所以当你在代码中设置它时:
$data['Video'][0]['name']='a';
$data['Video'][1]['name']='b';
$data['Video'][1]['name']='c';
$data['Video'][3]['name']='d';
$data['Video'][3]['name']='e';
It really just means this:
它真的只是意味着:
$data['Video'][0]['name']='a';
$data['Video'][1]['name']='c';
$data['Video'][3]['name']='e';
The newer data assigned to the keys 1
and 3
overwrite what was previously there.
分配给键1和3的较新数据会覆盖之前的数据。