0

how can i fetch array from inside other array using foreach php?

i had an array like that

        Array
(
    [0] => Array
        (
            [id] => 3
            [programs] => Internet Download Manager
            [version] => 6.05
            [type] => Internet
            [description] => dfsdfdsfdsfds
fds
f
sd
fds
fs

            [views] => 100
            [serial] => bbbbbb-sssss-dsdffff
yyyy-zzzz-xxxx
        )

)

but it could have more than array how can i fetch each one using foreach() function??

1
  • I'm not sure I understand what you're asking here. You can nest foreach loops - i.e. foreach ($array as $item) { foreach($item as $subitem) { //process subitems } } Commented Apr 6, 2011 at 22:49

2 Answers 2

1

The short answer is .. yes you can use foreach inside foreach.

foreach($arr as $k => $v){
  foreach($v as $k2=>$v2){
    foreach($v2 as $k3 => $v3){
      // go on
    }
  }
}

Edit: If you want to check if the value is an array .. use is_array($v)

2
  • can u paste the exact error here? if the top level array contains a mix of arrays and non-array values, you can use the if(is_array($v)) to check and then if true use the foreach, otherwise handle differently. Commented Apr 6, 2011 at 22:54
  • great .. BTW welcome to stack overflow .. the way to say thank you here is by upvoting and accepting the answer :) Commented Apr 6, 2011 at 22:58
0

foreach($something[0] as $foo => $bar) for your current example or you can look at something like http://debuggable.com/posts/xpath-on-php-arrays-set-extract:48ca6265-258c-4032-b3ff-55b84834cda3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.