php - Loop within loop used on multi-dimensional array to group data possible? -
i trying output grouped data multidimensional array. consider highest level array key 'group', within groups individual search lines.
e.g. $group[ 0 ][ 1 ]
'group 1, line 1'.
i want display lines each group, signify group has changed inserting '<hr />
' tag. @ moment first group displays properly, hr tag shown none of second group results display. method of loop within loop wrong? possible use multi dimensional array this? thanks!!
my array looks this:
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 ) ) )
my code looks this:
$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++; }
looking @ example array, y value of second group should start on 4 , loop until reaches 6.
i recommend using foreach loops. http://php.net/manual/en/control-structures.foreach.php
your code follows:
foreach ( $groups $gkey => $gvalue ) { foreach ( $gvalue $key => $value ) { echo $groups[$gkey][$key]->searchstring . "<br />"; // or $value->searchstring . "<br />"; } echo "<hr />"; }
Comments
Post a Comment