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

时间:2015-07-22 12:35:56

标签: php arrays 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 Parent的父类别Test ChildTest 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);


}

2 个答案:

答案 0 :(得分:2)

这应该适合您,确保结果中没有零索引项。我认为,通过将空值项与具有关联键的项合并,来自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);

答案 1 :(得分:1)

此解决方案适用于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);