【问题标题】:php array_walk multidimensional arrayphp array_walk 多维数组
【发布时间】:2012-01-06 20:08:14
【问题描述】:

我得到了三个带有某种分层预定义术语的数组

array("fruits", "yellow", "pineapple");
array("fruits", "yellow", "lemon");
array("fruits", "red", "apple");

我有一个 assoc 数组,它有一种层次结构:

array('fruits'=>array('red'=>array('tomato')));

如何将我的三个数组的术语推送到我得到的正确位置:

array('fruits'=>array('yellow'=>array('pineapple','lemon'),'red'=>array('tomato','apple')));

我是否使用 array_walk?还是array_walk_recursive? 我应该使用什么?

最好的,乔尔格

【问题讨论】:

    标签: php multidimensional-array hierarchy


    【解决方案1】:

    您将每个水果转换为嵌套数组,然后使用array_merge_recursive() 进行合并。

    这是一个工作示例 (also on Codepad):

    $fruits = array(
      array("fruits", "yellow", "pineapple"),
      array("fruits", "yellow", "lemon"),
      array("fruits", "red", "apple"),
      array("fruits", "red", "tomato"),
    );
    
    // Convert array to nested array
    function nest($leaf)
    {
      if (count($leaf) > 1)
      {
        $key = array_shift($leaf);
    
        return array($key => nest($leaf));
      }
      else
      {
        return $leaf;
      }
    }
    
    $tree = array();
    
    foreach($fruits as $fruit)
    {
      // Convert each fruit to a nested array and merge recursively
      $tree = array_merge_recursive($tree, nest($fruit));
    }
    
    print_r($tree);
    

    【讨论】:

    • 谢谢!而已!你让我开心:-)
    【解决方案2】:
    $fruits[] = array("fruits", "yellow", "pineapple");
    $fruits[] = array("fruits", "yellow", "lemon");
    $fruits[] = array("fruits", "red", "apple");
    
    foreach($fruits as $fruit) {
      $multifruit[$fruit[0]][$fruit[1]][] = $fruit[2];
    }
    
    print_r($multifruit);
    
    /* yields:
    Array
    (
        [fruits] => Array
            (
                [yellow] => Array
                    (
                        [0] => pineapple
                        [1] => lemon
                    )
    
                [red] => Array
                    (
                        [0] => apple
                    )
    
            )
    
    )
    */
    

    完全按照您的意愿行事。赋值左侧的最后一个 [] 附加在右侧,而不是覆盖任何现有值(如果存在)。

    【讨论】:

    • 我对这个答案的唯一问题是它不足以处理更长或更短的数组。如果你给它一个array('fruit','yellow','apple','granny smith')呢?
    【解决方案3】:
    <?php
    
    $fruits[] = array("fruits", "yellow", "pineapple");
    $fruits[] = array("fruits", "yellow", "lemon");
    $fruits[] = array("fruits", "red", "apple");
    $fruits[] = array("fruits", "blue", "small","blueberry");
    $fruits[] = array("fruits", "blue", "bluefruit");
    $fruits[] = array("fruits", "multicolor-fruit");
    
    function deeper(&$multifruit, $fruit) {
        if (count($fruit)>2) {
            $shifted = array_shift($fruit);
            deeper($multifruit[$shifted], $fruit);
            return $multifruit;
        } else {
            return $multifruit[$fruit[0]][] = $fruit[1];
        }   
    }   
    
    foreach($fruits as $fruit) {
       deeper($multifruit, $fruit);
    }   
    
    print_r($multifruit);
    ?>  
    

    在这里,您可以找到更通用的问题解决方案。我花了一段时间,所以我希望你能欣赏它:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 2018-06-13
      • 2022-01-07
      • 2013-03-04
      相关资源
      最近更新 更多