0

我有一个使用循环运行的数组,代码如下:

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 ) 
4

1 回答 1

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;
        }
}
于 2013-02-10T11:10:18.760 回答