【问题标题】:How to add and index dimensions of arrays in php dynamically?如何在php中动态添加和索引数组的维度?
【发布时间】:2022-01-15 06:36:53
【问题描述】:

我在一个 PHP 类中有一个数组和两个成员函数

第一个接收两个整数,一个是维度,另一个是值:

private $complexArray;

function setValueToGivenDimension($dimension, $value)

我想要做的是将值设置为数组的给定维度。

例如,如果我这样调用函数:

setValueToGivenDimension(3,"key","myValue")

我希望数组是

array [
  0 => array [
    0 => array [
      "key" => "myValue"
    ]
  ]
]

第二个是函数getValueOfGivenDimension($dimension, $key)

例如,如果我这样调用函数:

getValueOfGivenDimension(3,"key")

它应该返回给定键的值,在这种情况下 $complexArray 的第 3 维是 0:

"myValue"

*array 可以有任何级别的维度,我想动态创建和索引数组的维度。

【问题讨论】:

    标签: php arrays multidimensional-array


    【解决方案1】:

    这是一种方法。注意使用& 来获取数组的引用:

    function setValueToGivenDimension(&$array, $dimension, $key, $value)
    {
        for($i=1;$i<$dimension;$i++)
        {
            if(!isset($array[0]))
                $array[0] = [];
            $array = &$array[0];
        }
        $array[$key] = $value;
    }
    
    function getValueOfGivenDimension($array, $dimension, $key)
    {
        for($i=1;$i<$dimension;$i++)
        {
            if(!isset($array[0]))
                return null;
            $array = $array[0];
        }
        if(!isset($array[$key]))
            return null;
        return $array[$key];
    }
    
    $complexArray = [];
    setValueToGivenDimension($complexArray, 3, 'key', 'value');
    echo getValueOfGivenDimension($complexArray, 3, 'key');
    

    【讨论】:

      【解决方案2】:

      你可以同时使用递归。

      以下方法将返回您想要的数组,因此您可以将私有变量设置为输出。

      function setValueToGivenDimension($dimension, $value) {
           // Base case: if the dimension is 0, then we should return the value
           if ($dimension == 0) return $value;
           
           // If the dimension is greater than 0, then return the recursive function call, wrapped in a new array
           return [setValueToGivenDimension($dimension - 1, $value)];
      }
      

      以下方法将采用数组、维度和键,并使用最内维度中的键输出该数组的第 n 维的值。

      function getValueOfGivenDimension($array, $dimension, $key) {
           // Base case: if the function is at dimension 1, then return the value at the given key for the array
           if ($dimension == 1) return $array[$key];
           
           // If the dimension is greater than 0, then recursively call the function on the only child of the given array
           return getValueOfGivenDimension($array[0], $dimension - 1, $key);
      }
      

      【讨论】:

      • setValueToGivenDimension 每次都返回一个新数组,但我只想更新现有数组,如果我想用一个键设置一个值怎么办?
      • 您可以将我的定义用作辅助函数。您的实际函数可以简单地将您的数组设置为我的函数的值。
      【解决方案3】:

      根据 Olivers 的回答,这是一个类中的代码:

      <?php
      
      class MyClass
      {
          private $complexArray;
      
          /**
           * @return void
           */
          public function doSomething()
          {
              $this->setValueToGivenDimension(3, 'key', 'value');
              print_r($this->complexArray);
              echo $this->getValueOfGivenDimension(3, 'key') . PHP_EOL;
          }
      
          /**
           * @param int $dimension
           * @param mixed $key
           * @param mixed $value
           * @return $this
           */
          private function setValueToGivenDimension($dimension, $key, $value)
          {
              if (!is_array($this->complexArray)) {
                  $this->complexArray = [];
              }
              if ($dimension > 0) {
                  $array = &$this->complexArray;
                  for ($i = 1; $i < $dimension; $i++) {
                      if (!isset($array[0])) {
                          $array[0] = [];
                      }
                      $array = &$array[0];
                  }
                  $array[$key] = $value;
              }
              return $this;
          }
      
          /**
           * @param int $dimension
           * @param mixed $key
           * @return mixed|null
           */
          private function getValueOfGivenDimension($dimension, $key)
          {
              if ($dimension > 0) {
                  $array = $this->complexArray;
                  for ($i = 1; $i < $dimension; $i++) {
                      if (!isset($array[0])) {
                          return null;
                      }
                      $array = $array[0];
                  }
                  if (!isset($array[$key])) {
                      return null;
                  }
                  return $array[$key];
              }
              return null;
          }
      
      }
      

      运行它:

      $mc = new MyClass();
      $mc->doSomething();
      
      

      输出:

      Array
      (
          [0] => Array
              (
                  [0] => Array
                      (
                          [key] => value
                      )
      
              )
      
      )
      value
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多