检查是否数组是多维的或不? [英] Checking if array is multidimensional or not?

查看:171
本文介绍了检查是否数组是多维的或不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 什么是检查是否数组是最有效的方式平面阵列
    原始值
    或如果它是一个多维数组

  2. 有没有办法做到这一点,而无需经过实际循环
    阵列和运行 is_array()在它的每个元素?


解决方案

简短的回答是不,你不能这样做没有至少隐含的循环,如果第二维可以在任何地方。如果它是在第一个项目,你只是做

  is_array($改编[0]);

不过,我能找到的最有效的一般方法是使用foreach循环阵列上,只要击中发现短路(至少隐含的环比直()越好):

  $更多multi.php
< PHP$一个=阵列(1 =>'一',2 =>'B',3 =>阵列(1,2,3));
$ B =阵列(1 =>'一',2 =>'B');
$ C =阵列(1 =>'一',2 =>'B','富'=>阵列(1,阵列(2)));功能is_multi($ A){
    $ RV = array_filter($ A,is_array');
    如果(计数($ RV)0)返回true;
    返回false;
}功能is_multi2($ A){
    的foreach($ A作为V $){
        如果(is_array($ V))返回true;
    }
    返回false;
}功能is_multi3($ A){
    $ C =计数($ A);
    为($ I = 0; $ I< $ C,$ I ++){
        如果(is_array($ A [$ i]))返回true;
    }
    返回false;
}
$ iters = 500000;
$ =时间microtime中(真);
为($ I = 0; $ I< $ iters; $ I ++){
    is_multi($ A);
    is_multi($ B);
    is_multi($ C);
}
$结束= microtime中(真);
回声is_multi了($ $时间端值),在$ iters秒倍的\\ n。$ =时间microtime中(真);
为($ I = 0; $ I< $ iters; $ I ++){
    is_multi2($ A);
    is_multi2($ B);
    is_multi2($ C);
}
$结束= microtime中(真);
回声is_multi2了($ $时间端值),在$ iters秒倍的\\ n。
$ =时间microtime中(真);
为($ I = 0; $ I< $ iters; $ I ++){
    is_multi3($ A);
    is_multi3($ B);
    is_multi3($ C);
}
$结束= microtime中(真);
回声is_multi3了($ $时间端值),在$ iters秒倍的\\ n。
?>$ PHP multi.php
is_multi了7.53565130424秒500000次
is_multi2了4.56964588165秒500000次
is_multi3了9.01706600189秒500000次

隐式的循环,但我们不能尽快找到一个匹配的短路...

  $更多multi.php
< PHP$一个=阵列(1 =>'一',2 =>'B',3 =>阵列(1,2,3));
$ B =阵列(1 =>'一',2 =>'B');功能is_multi($ A){
    $ RV = array_filter($ A,is_array');
    如果(计数($ RV)0)返回true;
    返回false;
}后续代码var_dump(is_multi($ A));
后续代码var_dump(is_multi($ B));
?>$ PHP multi.php
布尔(真)
布尔(假)

  1. 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?
  2. Is there any way to do this without actually looping through an array and running is_array() on each of its elements?

解决方案

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

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

这篇关于检查是否数组是多维的或不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆