![](/img/trans.png)
[英]How to merge two two dimensional arrays with the same number of elements per sub array in php?
[英]Get the number of arrays in a two dimensional array
我目前正在使用redux数组中的wordpress的此功能 ,它让我的用户做的是创建一个数组,其中填充了可定制数量的嵌套数组(每个嵌套数组代表一张幻灯片)。
我不是最擅长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/zh/class.recursivearrayiterator.php
顺便说一句,如果您要表示的是可自定义数量的嵌套数组,例如在一个包含大量一级数组的巨型数组中,那么foreach
应该使您感到公平。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.