简体   繁体   中英

iterate through multidimentional array in php

I have multid array how to loop through each element, for the simple array we can do like this way

 $numbers = array( 1, 2, 3, 4, 5);
foreach( $numbers as $value )
{
  echo "Value is $value <br />";
}

so how to loop through following array

array(852,array(456,1234,784),array(102,896,121),array(1222,963,123),array(102,896,121),1234,array(102,896,121),676,54654,array(123,4785,123),array(41256,789),741,123,array(4561))

try this way

$array=array(852,array(456,1234,784),array(102,896,121),array(1222,963,123),array(102,896,121),1234,array(102,896,121),676,54654,array(123,4785,123),array(41256,789),741,123,array(4561));
    foreach( $array as $value )
    {
     if(is_array($value))
     {
         foreach($value as $v)
         {
             echo "Value is $v <br />";
         }
     }
     else
     {
         echo "Value is $value <br />";
     }
    }

Try this.

$arr = array(852, array(456, 1234, 784), array(102, 896, 121), array(1222, 963, 123), array(102, 896, 121), 1234, array(102, 896, 121), 676, 54654, array(123, 4785, 123), array(41256, 789), 741, 123, array(4561));

foreach ($arr as $val)
{
    if (is_array($val))
    {
        foreach ($val as $valIn){
            echo $valIn."<br>";
        }
    } else
    {
        echo $val."<br>";
    }
}        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM