php 检查数组是否是多维的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/145337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:39:56  来源:igfitidea点击:

Checking if array is multidimensional or not?

phparraysloopsmultidimensional-arrayelement

提问by Wilco

  1. What is the most efficient way to check if an array is a flat array of primitive valuesor if it is a multidimensional array?
  2. Is there any way to do this without actually looping through an array and running is_array()on each of its elements?
  1. 检查数组是原始值的平面数组还是多维数组的最有效方法是什么?
  2. 有没有办法在不实际遍历数组并is_array()在其每个元素上运行的情况下执行此操作?

采纳答案by Vinko Vrsalovic

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

简短的回答是不,如果“第二维”可以在任何地方,那么至少没有隐式循环就无法做到这一点。如果它必须在第一项中,你就做

is_array($arr[0]);

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

但是,我能找到的最有效的一般方法是在数组上使用 foreach 循环,只要找到命中就短路(至少隐式循环比直接 for() 更好):

$ more multi.php
<?php

$a = array(1 => 'a',2 => 'b',3 => array(1,2,3));
$b = array(1 => 'a',2 => 'b');
$c = array(1 => 'a',2 => 'b','foo' => array(1,array(2)));

function is_multi($a) {
    $rv = array_filter($a,'is_array');
    if(count($rv)>0) return true;
    return false;
}

function is_multi2($a) {
    foreach ($a as $v) {
        if (is_array($v)) return true;
    }
    return false;
}

function is_multi3($a) {
    $c = count($a);
    for ($i=0;$i<$c;$i++) {
        if (is_array($a[$i])) return true;
    }
    return false;
}
$iters = 500000;
$time = microtime(true);
for ($i = 0; $i < $iters; $i++) {
    is_multi($a);
    is_multi($b);
    is_multi($c);
}
$end = microtime(true);
echo "is_multi  took ".($end-$time)." seconds in $iters times\n";

$time = microtime(true);
for ($i = 0; $i < $iters; $i++) {
    is_multi2($a);
    is_multi2($b);
    is_multi2($c);
}
$end = microtime(true);
echo "is_multi2 took ".($end-$time)." seconds in $iters times\n";
$time = microtime(true);
for ($i = 0; $i < $iters; $i++) {
    is_multi3($a);
    is_multi3($b);
    is_multi3($c);
}
$end = microtime(true);
echo "is_multi3 took ".($end-$time)." seconds in $iters times\n";
?>

$ php multi.php
is_multi  took 7.53565130424 seconds in 500000 times
is_multi2 took 4.56964588165 seconds in 500000 times
is_multi3 took 9.01706600189 seconds in 500000 times

Implicit looping, but we can't shortcircuit as soon as a match is found...

隐式循环,但我们不能在找到匹配项后立即短路......

$ more multi.php
<?php

$a = array(1 => 'a',2 => 'b',3 => array(1,2,3));
$b = array(1 => 'a',2 => 'b');

function is_multi($a) {
    $rv = array_filter($a,'is_array');
    if(count($rv)>0) return true;
    return false;
}

var_dump(is_multi($a));
var_dump(is_multi($b));
?>

$ php multi.php
bool(true)
bool(false)

回答by

Use count() twice; one time in default mode and one time in recursive mode. If the values match, the array is notmultidimensional, as a multidimensional array would have a higher recursive count.

使用 count() 两次;一次在默认模式下,一次在递归模式下。如果值匹配,则该数组不是多维数组,因为多维数组将具有更高的递归计数。

if (count($array) == count($array, COUNT_RECURSIVE)) 
{
  echo 'array is not multidimensional';
}
else
{
  echo 'array is multidimensional';
}

This option second value modewas added in PHP 4.2.0. From the PHP Docs:

这个选项的第二个值mode是在 PHP 4.2.0 中添加的。来自PHP 文档

If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array. count() does not detect infinite recursion.

如果可选模式参数设置为 COUNT_RECURSIVE(或 1),count() 将递归计算数组。这对于计算多维数组的所有元素特别有用。count() 不检测无限递归。

However this method does not detect array(array()).

但是这种方法检测不到array(array()).

回答by scronide

For PHP 4.2.0 or newer:

对于 PHP 4.2.0 或更新版本:

function is_multi($array) {
    return (count($array) != count($array, 1));
}

回答by Andreas

I think this is the most straight forward way and it's state-of-the-art:

我认为这是最直接的方法,它是最先进的:

function is_multidimensional(array $array) {
    return count($array) !== count($array, COUNT_RECURSIVE);
}

回答by Pian0_M4n

You can simply execute this:

你可以简单地执行这个:

if (count($myarray) !== count($myarray, COUNT_RECURSIVE)) return true;
else return false;

If the optional mode parameter is set to COUNT_RECURSIVE(or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array.

如果可选的 mode 参数设置为COUNT_RECURSIVE(或 1),count() 将递归计算数组。这对于计算多维数组的所有元素特别有用。

If it's the same, means there are no sublevels anywhere. Easy and fast!

如果相同,则意味着任何地方都没有子级别。简单快捷!

回答by Greg Hewgill

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.

您可以检查is_array()第一个元素,假设如果数组的第一个元素是数组,那么其余元素也是。

回答by Joe

All great answers... here's my three lines that I'm always using

所有很好的答案......这是我一直在使用的三行

function isMultiArray($a){
    foreach($a as $v) if(is_array($v)) return TRUE;
    return FALSE;
}

回答by Joe

This function will return int number of array dimensions (stolen from here).

此函数将返回数组维数的整数(从此处窃取)。

function countdim($array)
{
   if (is_array(reset($array))) 
     $return = countdim(reset($array)) + 1;
   else
     $return = 1;

   return $return;
}

回答by RoboTamer

I think you will find that this function is the simplest, most efficient, and fastest way.

我想你会发现这个功能是最简单、最高效、最快捷的方式。

function isMultiArray($a){
    foreach($a as $v) if(is_array($v)) return TRUE;
    return FALSE;
}

You can test it like this:

你可以这样测试:

$a = array(1 => 'a',2 => 'b',3 => array(1,2,3));
$b = array(1 => 'a',2 => 'b');

echo isMultiArray($a) ? 'is multi':'is not multi';
echo '<br />';
echo isMultiArray($b) ? 'is multi':'is not multi';

回答by Prashant

You can also do a simple check like this:

你也可以做一个这样的简单检查:

$array = array('yo'=>'dream', 'mydear'=> array('anotherYo'=>'dream'));
$array1 = array('yo'=>'dream', 'mydear'=> 'not_array');

function is_multi_dimensional($array){
    $flag = 0;
    while(list($k,$value)=each($array)){
        if(is_array($value))
            $flag = 1;
    }
    return $flag;
}
echo is_multi_dimensional($array); // returns 1
echo is_multi_dimensional($array1); // returns 0