获取二维数组中的数组数


Get the number of arrays in a two dimensional array

我目前正在使用wordpress redux array的这个功能,它让我的用户做的是创建一个数组,填充可定制数量的嵌套数组(每个嵌套数组代表一张幻灯片)

我不擅长php,这是一个非常简单的问题,但是,如果我在foreach($mybigarray as $value)中运行它们,$value是否会成为每个子数组,所以我可以通过这个方法打印所有子数组的值:

foreach($mybigarray as $value){
  echo $value['1'];
  echo $value['2'];
}

如果这是假的,我将如何通过 If 语句运行每个子数组(数量未知)。有什么"最佳实践"是我应该知道的吗?

谢谢!

示例数组的输出:

Array ( 
  [0] => Array (
    [title] => Title #1
    [description] => Sub Title Text #1
    [url] => Link #1
    [sort] => 1
    [attachment_id] => 1461
    [image] => http://demo.pixelcrescent.com/LeagueOfLegends/wp-content/uploads/2014/06/mountains.jpg
    [height] => 1200
    [width] => 1920
    [thumb] => http://demo.pixelcrescent.com/LeagueOfLegends/wp-content/uploads/2014/06/mountains-150x150.jpg
  )[1] => Array (
    [title] => Title #2
    [description] => Sub Title Text #2
    [url] => Link #2
    [sort] => 2
    [attachment_id] => 1461
    [image] => http://demo.pixelcrescent.com/LeagueOfLegends/wp-content/uploads/2014/06/mountains.jpg
    [height] => 1200
    [width] => 1920
    [thumb] => http://demo.pixelcrescent.com/LeagueOfLegends/wp-content/uploads/2014/06/mountains-150x150.jpg
  )[2] => Array (
    [title] => Title #3
    [description] => Sub Title Text #3
    [url] => link #3
    [sort] => 3
    [attachment_id] => 1461
    [image] => http://demo.pixelcrescent.com/LeagueOfLegends/wp-content/uploads/2014/06/mountains.jpg
    [height] => 1200
    [width] => 1920
    [thumb] => http://demo.pixelcrescent.com/LeagueOfLegends/wp-content/uploads/2014/06/mountains-150x150.jpg
  )
)

如果我通过foreach($mybigarray as $value)来运行它们,$value会变成每个子数组

吗?

是的,每个$value将成为子数组。

然而,正如你所说的,如果它有可定制数量的嵌套数组(数组中的数组中的数组…),你必须编写你自己的递归多数组读取器,或者检查已经存在的。

试试下面这个例子:http://www.php.net//manual/en/class.recursivearrayiterator.php

顺便说一下,如果你的意思是可定制数量的嵌套数组,就像在一个巨大的数组中有很多一级数组,那么foreach应该做你的正义。