是否可以对此数组进行分组?

时间:2012-07-07 13:04:48

标签: php arrays grouping

我有一个数组,我需要在多级数组中对此数组进行排序。我正在尝试按其字段对其进行分组,但我可以使其工作。这是我拥有的数组和我想要的例子

Array
(
    [0] => Array
        (
            [id] => sports
            [title] => this is sports
        )

    [1] => Array
        (
            [id] => cricket
            [title] => this is cricket
            [under] => sports
        )

    [2] => Array
        (
            [id] => batsman
            [title] => this is batsman
            [under] => cricket
        )

    [3] => Array
        (
            [id] => sachin
            [title] => this is sachin
            [under] => batsman
        )

    [4] => Array
        (
            [id] => football
            [title] => this is football
            [under] => sports
        )

    [5] => Array
        (
            [id] => ronaldo
            [title] => this is ronaldo
            [under] => football
        )

)

我需要对此数组进行分组并使其像这样

Array(
    [0] => Array(
        [id] => Array(
            [sports] => Array(
                [cricket] => Array(
                    [batsman] => sachin
                )
                [football] => fun
            )
        )
    )
)

我尝试了类似的东西,但它无法正常工作

foreach($my_array as $item) {
    //group them by under
    $my_grouped_array[$item['under']][] = $item;
}

任何建议都会很棒。

3 个答案:

答案 0 :(得分:0)

我编写了一个递归函数,可以执行您想要的操作,但请记住,如果您有多个分支的最后一个元素,则只保存第一个元素。

这是功能:

function rearrange(&$result, $my_array, $element = NULL)
{
        $found = 0;
        $childs = 0;
        foreach($my_array as $one) if(@$one['under'] == $element)
        {
                $found++;
                if( ! is_array($result)) $result = array();
                $result[$one['id']] = $one['id'];

                $childs += rearrange($result[$one['id']], $my_array, $one['id']);
        }
        if( ! $childs AND is_array($result))
                $result = reset($result);

        return $found;
}

您可以这样称呼它:

$result = array(array('id' => array()));
rearrange($result[0]['id'], $my_array);
print_r($result);

答案 1 :(得分:0)

我认为这是最直接的做法:

function getChildren($entry,$by_parent){
    $children = array();
    if (isset($by_parent[$entry['id']])){
        foreach ($by_parent[$entry['id']] as $child){
            $id = $child['id'];
            $children[$id] = getChildren($child,$by_parent);
        }
    }
    return $children;
}

$by_parent = array();
$roots = array();
foreach ($array as $entry){
    if (isset($entry['under'])){
        $by_parent[$entry['under']][] = $entry;
    } else {
        $roots[] = $entry;
    }
}
$result = array();
foreach ($roots as $entry){
    $id = $entry['id'];
    $result[$id] = getChildren($entry,$by_parent);
}
$results = array(array('id'=>$results));

注意:这不是完全问题中指定的格式,但问题没有定义如何处理具有相同父节点的多个叶节点,这应该更容易遍历无论如何,因为它更加一致。

答案 2 :(得分:-1)

使用php对象:

    function populateArray($my_array) {
    //Populate the array
    while ($my_array as $item) {
            $array[$item->id]['id'] = $obj->id;
            $array[$item->id]['name'] = $obj->name;
        }  
     return $array;
     }

$a = populateArray($array);    
echo $a[0]['id'].'<br />';
echo $a[0]['name'].'<br />';

或使用新的foreach