【问题标题】:Determine number of Dimensions in a PHP Array确定 PHP 数组中的维数
【发布时间】:2011-01-08 11:54:44
【问题描述】:

有没有办法确定 PHP 数组中有多少维?

【问题讨论】:

标签: php


【解决方案1】:

您可以使用以下单行语句来区分一维数组和二维数组

if (gettype(reset($array)) == "array")

这对于二维数组返回 true,对于一维数组返回 false。

【讨论】:

    【解决方案2】:

    这是一个对我有用的解决方案,用于获取不均匀分布的数组的维数。

    function count_dimension($array, $count = 0) {
    $maxcount = 0;
    foreach ($array as $key) {
        if(is_array($key)) {
            $count = count_dimension(current($key), ++$count);
            if($count > $maxcount) {
                $maxcount = $count;
            }
        } else {
            if($count > $maxcount) {
                $maxcount = $count;
            }
        }
    }
    
    return $maxcount;}
    

    【讨论】:

      【解决方案3】:

      如果只有最里面的数组有项目,可以使用如下函数:

      function array_dim($array = []) {
          $dim = 0;
          $json = json_encode($array);
          $json_last_index = strlen($json) - 1;
      
          while (in_array($json[$json_last_index - $dim], ['}', ']'])) {
              $dim++;
          }
      
          return $dim;
      }
      

      如果要计算最大数组维度,可以使用以下函数:

      function max_array_dim($array = []) {
          $json = json_encode($array);
      
          $step = 0;
          $max = 0;
          for ($i = 0; $i < strlen($json); $i++) {
              if (in_array($json[$i], ['[', '{'])) {
                  $step++;
              }
              if (in_array($json[$i], [']', '}'])) {
                  $step--;
              }
              $max = max($max, $step);
          }
      
          return $max;
      }
      

      【讨论】:

        【解决方案4】:

        已在 Some issues with jumping from one function to another in a loop in php 更正


        这个 double 函数将转到 $a 中每个数组的最后一维,当它不再是一个数组时,它将回显它使用分隔符 | 到达那里的循环数。 这段代码的缺点是它只回显并且不能返回(以正常方式)。

        function cc($b, $n)
        {
            $n++.' ';
            countdim($b, $n);
        
        }
        function countdim($a, $n = 0)
        {
            if(is_array($a))
            {
                foreach($a as $b)
                {
                    cc($b, $n);
                }
            }else
            {
                echo $n.'|';
            }
        }
        countdim($a);
        

        我在这里创建了一个返回函数,但是..它是从 html 返回然后在按钮单击时“GET”返回到 php.. 我不知道任何其他方法可以使它工作.. 所以只需将您的数组命名为 $a 并点击按钮:/

        $max_depth_var = isset($_REQUEST['max_depth_var']) ? $_REQUEST['max_depth_var'] : 0;
        ?>
        <form id="form01" method="GET">
        <input type="hidden" name="max_depth_var" value="<?php
        function cc($b, $n)
        {
            $n++.' ';
            bb($b, $n);
        }
        function bb($a, $n = 0)
        {
            if(is_array($a))
            { 
                foreach($a as $b)cc($b, $n); 
            }else
            {
            echo $n.', ';
            };
        }
        bb($a); ?>">
        <input type="submit" form="form01" value="Get max depth value">
        </form><?php
        $max_depth_var = max(explode(', ', rtrim($max_depth_var, ",")));
        echo "Array's maximum dimention is $max_depth_var.";
        

        【讨论】:

          【解决方案5】:

          这适用于每个维度没有相同类型元素的数组。它可能需要遍历所有元素。

          $a[0] = 1; $a[1][0] = 1; $a[2][1][0] = 1; 函数 array_max_depth($array, $depth = 0) { $max_sub_depth = 0; foreach (array_filter($array, 'is_array') as $subarray) { $max_sub_depth = 最大值( $max_sub_depth, array_max_depth($subarray, $depth + 1) ); } 返回 $max_sub_depth + $depth; }

          【讨论】:

            【解决方案6】:

            与大多数过程和面向对象的语言一样,PHP 本身并不实现多维数组 - 它使用嵌套数组。

            别人建议的递归函数比较乱,但最接近答案。

            C.

            【讨论】:

              【解决方案7】:

              你可以试试这个:

              $a["one"]["two"]["three"]="1";
              
              function count_dimension($Array, $count = 0) {
                 if(is_array($Array)) {
                    return count_dimension(current($Array), ++$count);
                 } else {
                    return $count;
                 }
              }
              
              print count_dimension($a);
              

              【讨论】:

                【解决方案8】:

                好问题,这里是a solution I stole from the PHP Manual

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

                【讨论】:

                • 这并不完全正确。因为它只测试数组的第一个元素。因此,当您确定它是一个均匀分布的数组时,这只给出了预期的结果。您必须遍历所有元素才能真正了解可变深度。 (或者也许是一些我不知道的漂亮的遍历算法)
                猜你喜欢
                • 1970-01-01
                • 2017-01-19
                • 1970-01-01
                • 2018-12-26
                • 1970-01-01
                • 1970-01-01
                • 2018-02-13
                • 2011-10-18
                • 1970-01-01
                相关资源
                最近更新 更多