timmyd Posted December 25, 2007 Share Posted December 25, 2007 i have this array: [76] => Array ( [463] => 11154 [464] => 11155 [679] => 11634 ) [77] => Array ( [469] => 11172 [586] => 11427 [588] => 11429 [883] => 11981 [886] => 11987 [891] => 11997 [893] => 11999 [894] => 12000 [895] => 12001 [3078] => 15405 [3079] => 15406 ) [74] => Array ( [606] => 11466 [632] => 11515 [1604] => 13419 ) now i want to do a foreach on id 74 forexample the result should be: 1.11466 2.11515 3.13419 how i am gonna adchieve this cos with this foreach($array_ids_per_user as $key => $value){ //print $value; print "xxxx .".$key." ".$value[2]."<br>"; } didnt work out. hope you can help out cos i am new to arrays Quote Link to comment https://forums.phpfreaks.com/topic/83116-array-foreach-issue/ Share on other sites More sharing options...
PHP_PhREEEk Posted December 25, 2007 Share Posted December 25, 2007 You have a nested array, so you just need to embed another foreach: <?php $array_ids_per_user = array(74 => array(606 => 11466, 632 => 11515, 1604 => 13419)); echo "<pre>"; print_r($array_ids_per_user); echo "</pre><br>"; foreach( $array_ids_per_user as $key => $value ) { foreach ( $value as $key2 => $value2 ) { echo $key2 . " " . $value2 . "<br>"; } } // or echo "<br>"; $count = 1; foreach( $array_ids_per_user as $key => $value ) { foreach ( $value as $key2 => $value2 ) { echo $count . " " . $value2 . "<br>"; $count++; } } Output: Array ( [74] => Array ( [606] => 11466 [632] => 11515 [1604] => 13419 ) ) 606 11466 632 11515 1604 13419 1 11466 2 11515 3 13419 PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/83116-array-foreach-issue/#findComment-422790 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.