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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.