检查数组是否是多维的?
- 检查数组是否为平面数组的最有效方法是什么
原始值还是一个多维数组? - 有没有办法在不实际循环的情况下做到这一点
array 并对其每个元素运行is_array()
?
- What is the most efficient way to check if an array is a flat array
of primitive values or if it is a multidimensional array? - Is there any way to do this without actually looping through an
array and runningis_array()
on each of its elements?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
使用 count() 两次; 一次在默认模式下,一次在递归模式下。 如果值匹配,则该数组不是多维,因为多维数组将具有更高的递归计数。
该选项第二个值
mode
是在 PHP 4.2.0 中添加的。 来自 PHP 文档:但是此方法不会检测
array(array())
。Use count() twice; one time in default mode and one time in recursive mode. If the values match, the array is not multidimensional, as a multidimensional array would have a higher recursive count.
This option second value
mode
was added in PHP 4.2.0. From the PHP Docs:However this method does not detect
array(array())
.简短的答案是否定的,如果“第二维度”可能在任何地方,那么如果不至少隐式循环,您就无法做到这一点。 如果它必须在第一项中,你就这样做
但是,我能找到的最有效的通用方法是在数组上使用 foreach 循环,每当找到命中时就短路(至少隐式循环比直接 for()):
隐式循环,但我们不能在找到匹配项后立即短路...
The short answer is no you can't do it without at least looping implicitly if the 'second dimension' could be anywhere. If it has to be in the first item, you'd just do
But, the most efficient general way I could find is to use a foreach loop on the array, shortcircuiting whenever a hit is found (at least the implicit loop is better than the straight for()):
Implicit looping, but we can't shortcircuit as soon as a match is found...
对于 PHP 4.2.0 或更高版本:
For PHP 4.2.0 or newer:
我认为这是最直接的方法,也是最先进的:
I think this is the most straight forward way and it's state-of-the-art:
PHP 7 之后你可以简单地这样做:
After PHP 7 you could simply do:
您可以检查第一个元素的 is_array() ,假设如果数组的第一个元素是数组,那么其余元素也是数组。
You could look check
is_array()
on the first element, under the assumption that if the first element of an array is an array, then the rest of them are too.我想你会发现这个功能是最简单、最高效、最快的方式。
你可以这样测试:
I think you will find that this function is the simplest, most efficient, and fastest way.
You can test it like this:
不要使用 COUNT_RECURSIVE
点击此网站了解原因
使用 rsort 和然后使用 isset
Don't use COUNT_RECURSIVE
click this site for know why
use rsort and then use isset
即使这样,
如果false它是一个单维数组,如果true它是一个多维数组。
current 将为您提供数组的第一个元素,并通过 is_array 函数检查第一个元素是否是数组。
Even this works
If false its a single dimension array if true its a multi dimension array.
current will give you the first element of your array and check if the first element is an array or not by is_array function.
您还可以像这样进行简单的检查:
You can also do a simple check like this:
我认为这个很优雅(向另一位我不知道他的用户名的用户提供支持):
I think this one is classy (props to another user I don't know his username):
就我而言。 我陷入了各种奇怪的状况。
第一种情况 =
array("data"=> "name");
第二种情况 =
array("data"=> array("name"=>"username","fname"=>"fname"));
但是,如果
data
有数组而不是值,则 sizeof() 或 count() 函数不适用于这种情况。 然后我创建自定义函数来检查。如果数组的第一个索引有值,那么它返回“唯一值”
但如果索引有数组而不是值,那么它返回“有数组”
我使用这种方式
特别感谢 Vinko Vrsalovic
In my case. I stuck in vary strange condition.
1st case =
array("data"=> "name");
2nd case =
array("data"=> array("name"=>"username","fname"=>"fname"));
But if
data
has array instead of value then sizeof() or count() function not work for this condition. Then i create custom function to check.If first index of array have value then it return "only value"
But if index have array instead of value then it return "has array"
I use this way
Special thanks to Vinko Vrsalovic
它就像
Its as simple as
此函数将返回 int 数组维度数(从 此处)。
This function will return int number of array dimensions (stolen from here).
尝试如下
Try as follows
这是一个很好的衬里。 它迭代每个键以检查该键的值是否是数组。 这将确保真实
Here is a nice one liner. It iterates over every key to check if the value at that key is an array. This will ensure true