我有一个数组,我使用循环来运行,代码如下:
foreach($arry as $parentkey => $parentvalue){
$secondloop = explode(",",$parentvalue);
foreach($secondloop as $childvalue){
echo $parentkey.' '.$childvalue ;
}
}
当我运行它时,它不会显示父键。 php不支持那种循环吗? 如何让它显示父键?什么是最好的方式来循环以获得所需的结果?
原始数组
Array ( [1] => 2,3,10,11,27,28,35,36,165,37,38,40,41,42,43,44,46,49,50,61,62,65,66,75,67,71,69,72,73,74,76,96,90,91,97,107,118,147,119,122,139,142,148,149,168,169,170,171,172,173,174,181 [2] => 39,102,94,98,92,121 [3] => 45,77,117,103,109,99 [4] => 47,78,146,105,113,115,104 [5] => 48,79,106,114,120,110 [6] => 68,93,116,111,112 [7] => 140,150 [8] => 141,151 [9] => 143,144,166,153 [10] => 145,154,159 [11] => 157,155 [12] => 158,156 [13] => 160 [14] => 161 [15] => 162 [16] => 163 [17] => 164 )
答案 0 :(得分:1)
根据给出的信息,您的代码可以运行,请参阅以下清理的示例。
<?php
$arry = array(
1 => '2,3,10,11,27,28,35,36,165,37,38,40,41,42,43,44,46,49,50,61,62,65,66,75,67,71,69,72,73,74,76,96,90,91,97,107,118,147,119,122,139,142,148,149,168,169,170,171,17$
2 => '39,102,94,98,92,121',
3 => '45,77,117,103,109,99',
4 => '47,78,146,105,113,115,104',
5 => '48,79,106,114,120,110',
6 => '68,93,116,111,112',
7 => '140,150',
8 => '141,151',
9 => '143,144,166,153',
10 => '145,154,159',
11 => '157,155',
12 => '158,156',
13 => '160',
14 => '161',
15 => '162',
16 => '163',
17 => '164'
);
foreach($arry as $parentkey => $parentvalue){
$secondloop = explode(",",$parentvalue);
foreach($secondloop as $childvalue){
echo 'Parent key: ' . $parentkey . ', child value: ' . $childvalue . PHP_EOL;
}
}