0

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

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;
}

任何建议都会很棒。

4

3 回答 3

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);
于 2012-07-09T07:11:32.123 回答
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));

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

于 2012-07-09T09:39:58.057 回答
-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

于 2012-07-07T13:12:12.417 回答