我想從多維數組輸出分組數據。考慮最高級別的數組鍵「組」,然後在組內是單獨的搜索行。在多維數組上使用的循環內的循環可以將數據分組嗎?
例如$group[ 0 ][ 1 ]
將是'組1,行1'。
我想顯示來自每個組的所有行,然後通過插入'<hr />
'標籤表示該組已更改。此時第一組顯示正確,然後顯示hr標籤,但不顯示第二組結果。我的循環內循環方法是否錯誤?是否有可能像這樣使用多維數組? 謝謝!
我的數組是這樣的:
Array
(
[0] => Array
(
[0] => stdClass Object
(
[name] => testing
[searchID] => 131
[lineID] => 190
[searchString] => 1
)
[1] => stdClass Object
(
[name] => testing
[searchID] => 131
[lineID] => 191
[searchString] => 2
)
[2] => stdClass Object
(
[name] => testing
[searchID] => 131
[lineID] => 192
[searchString] => 3
)
[3] => stdClass Object
(
[name] => testing
[searchID] => 131
[lineID] => 193
[searchString] => 4
)
)
[1] => Array
(
[4] => stdClass Object
(
[name] => test2
[searchID] => 132
[lineID] => 199
[searchString] => 1
)
[5] => stdClass Object
(
[name] => test2
[searchID] => 132
[lineID] => 200
[searchString] => 2
)
)
)
我的代碼如下所示:
$x = 0;
$y = 0;
while($x < count($groups))
{
while($y < count($groups[ $x ]))
{
//display each single search string
echo $groups[ $x ][ $y ]->searchString.'<br>';
$y++;
}
echo '<hr>';
$x++;
}
這種方式比我的方式更簡單更優雅。我一定會在將來這樣做,謝謝! :) – 2013-04-04 15:14:20