在php中迭代多维数组

时间:2014-11-03 07:15:48

标签: php arrays sorting

我有多个数组如何遍历每个元素,对于我们可以这样做的简单数组

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

所以如何遍历以下数组

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))

2 个答案:

答案 0 :(得分:1)

试试这种方式

$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 />";
     }
    }

答案 1 :(得分:0)

试试这个。

$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>";
    }
}