基于数组键创建单独的子数组


Create seperate sub array based on array key

我需要根据>在数组键中出现的次数将单个数组分为子数组,这样我就可以判断谁是父类别,谁不是。请注意,可能嵌套的父对象的数量没有限制。

此外,如果存在具有相同名称的子项,则如果该子项具有不同的父项,则该子项将被视为唯一的。

我的源数组结构如下:

array (
  'Test Parent 2>Test Child>Test Sub Child' => 
  array (
    'content_id_4' => NULL,
  ),
  'Test Parent 3' => 
  array (
    'content_id_4' => NULL,
    'content_id_5' => NULL,
  ),
  'Test Parent>Test Child>Test Sub Child' => 
  array (
    'content_id_3' => NULL,
  ),
  'Test Parent 2 with No Kids' => 
  array (
    'content_id_3' => NULL,
  ),
  'Collections>Sports' => 
  array (
    'content_id_2' => NULL,
    'content_id_22' => NULL,
  ),
  'Collections' => 
  array (
    'content_id_2' => NULL,
    'content_id_22' => NULL,
    'content_id_6' => NULL,
  ),
  'Collections>Charity' => 
  array (
    'content_id_6' => NULL,
  ),
)

在上面的示例中,Test Parent>Test Child>Test Sub Child意味着存在具有子类别Test Child的父类别Test ParentTest Child也是一个父级,并且有一个名为Test Sub Child的子级,该子级没有任何子级。

所需输出示例:

array (
  'Collections' => 
  array (
    'Sports' => NULL,
    'Charity' => NULL,
  ),
  'Test Parent' => 
  array (
    'Test Child' => 
    array (
      'Test Sub Child' => NULL,
    ),
  ),
  'Test Parent 2 with No kids' => NULL,
  'Study' => 
  array (
    'Study Groups' => NULL,
  ),
)

我尝试了一个解决方案,但无法获得正确的语法,这样我就可以用孩子的孩子创建一个额外的数组。

我不一定要求重构我的示例。我只是在寻找最有效的解决方案。

我的示例代码

$category_structure = array();
foreach($event_categories as $main_cat => $content_ids) {
    $this_category_list = explode('>', $main_cat);
    $this_cat = array();
    $this_parent = array_shift($this_category_list);

    foreach($this_category_list as $cat) {
        $this_cat[$this_parent][$cat] = null;
    }
    $category_structure = array_merge_recursive($this_cat, $category_structure);

}

这应该对您有效,确保结果中没有零索引项。我认为它来自array_merge_recursive,通过将空值项与具有关联键的项合并。

不过,它不如P0rnflake的解决方案那么优雅,但我相信你会明白的。

$collect = array();
$result = array();
$last = "";
foreach($event_categories as $main_cat => $content_ids) {
    if (strpos($last, $main_cat) === false) {
        array_push($collect, explode('>', $main_cat));
    }
    $last = $main_cat;
} 
array_walk($collect, function($value) use (&$result) {
    $out = array();
    $cur = &$out;
    foreach ($value as $array) {
        if (count($value) !== 1) {
            $cur[$array] = array();
        } else {
            $cur[$array] = null;    
        }
        $cur = &$cur[$array];
    }
    $cur = null;
    $result = array_merge_recursive($result, $out);
});
var_dump($result);

此解决方案应适用于php>=5.3.0($yourArray是输入数组(:

// anonymous recursive function which merges a flat numeric array 
// into a hierarchy, f.e. array('parent','child','grandchild')
// will be built to a hierarchical array
$treeBuilder = function($numArray) use (&$treeBuilder) {
    if(isset($numArray[1])) {
        //recursive merge needed, there are still entries left
        return array(
            $numArray[0] => $treeBuilder(array_slice($numArray, 1))
        );
    }
    //end is reached
    return array(
        $numArray[0] => null
    );
};
$result = array();
foreach (array_keys($yourArray) as $key) {
    // loop through exploded keys and merge results
    $hierarchy = explode('>', $key);
    $result = array_merge_recursive($result, $treeBuilder($hierarchy));
}
var_dump($result);