Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array looping issue

Tags:

arrays

php

I have the following array

Array(
Array
(
    [Segment] => Array
        (
            [id] => 738
    )
),
Array
(
    [Segment] => Array
        (
            [0] => array([id] => 740),
            [1] => array([id] => 750)
    )
)
)

how can i loop the array. The second value need inner loop.

i need the output as

first loop as id->738

second loop as id->740, id->750

Regards, Nisanth

like image 376
Nisanth Kumar Avatar asked Jul 14 '26 19:07

Nisanth Kumar


1 Answers

You can do it like this:

foreach($array as $a) {
    foreach($a as $segment => $array) {
        if(isset($array['id'])) {
           echo $array['id']; //if there is an `id` index echo it
        } else {
           foreach($array as $k => $v) { //or else.. start looping again
               echo $v['id'];
           }
        }
    }
}
like image 154
Starx Avatar answered Jul 17 '26 15:07

Starx