Jump to content

Array foreach issue


timmyd

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/83116-array-foreach-issue/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/83116-array-foreach-issue/#findComment-422790
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.