对数组进行排序并重建它

时间:2014-09-03 07:56:09

标签: php arrays merge

我有这个数组:

    $all = array(
            'meat' => Object(
                        'name' => 'meat',
                        'color' => 'red',
                        'class' => 'food'
                ),
            'chicken' => Object(
                        'name' => 'chicken',
                        'color' => 'white',
                        'class' => 'food'
                ),
            'apple' => Object(
                        'name' => 'apple',
                        'color' => 'green',
                        'class' => 'Fruit'
                ),
            'blueberry' => Object(
                        'name' => 'blueberry',
                        'color' => 'blue',
                        'class' => 'Fruit'
                )
    );

我希望对其进行排序并将其重建为:

    $theright = array(
                array(
                    'class' => 'food',
                    'menu' => array(
                                array(
                                    'name' => 'meat',
                                    'color' => 'red',
                                ),
                                array(
                                    'name' => 'chicken',
                                    'color' => 'white',
                                )

                            )
                    ),
                array(
                    'class' => 'Fruit',
                    'menu' => array(
                                array(
                                    'name' => 'apple',
                                    'color' => 'green',
                                ),
                                array(
                                    'name' => 'blueberry',
                                    'color' => 'blue',
                                )
                        )
                    )
            );

我尝试收集$all数组中的所有类,然后将每个值与$all数组进行比较:

$classArray = array();
foreach($all as $key => $value) {
    $classArray[$value->class] = array();
}
foreach($classArray as $key => $value) {
    $theright[] = array('class' => $key, 'menu' => array());
}

这段代码给我这个数组:

$theright = array(
                array(
                    'class' => 'food',
                    'menu' => array()
                    ),
                array(
                    'class' => 'Fruit',
                    'menu' => array()
                    )
            );

我停在这里,如何完成它?

3 个答案:

答案 0 :(得分:1)

您可以使用该类作为键将它们组合在一起。例如:

$food = array();
// gather class
foreach($all as $item) {
    if(!isset($food[$item->class])) {
        $food[$item->class] = array(
            'class' => $item->class,
            'menu' => array(
                array(
                    'name' => $item->name,
                    'color' => $item->name,
                )
            )
        );
    } else {
        $food[$item->class]['menu'][] = array('name' => $item->name,'color' => $item->color,);
    }
}

// simple reindex
$food = array_values($food);

答案 1 :(得分:0)

不需要第二个循环。这应该做你想要的。

$classMap = array();

foreach ($all as $item)
{
    // check if class has been created in the class map
    if ( ! array_key_exists($classMap, $item['class']))
    {
        $classMap[$item['class']] = array(
            'class' => $item['class'],
            'menu'  => array()
        );
    }

    $classMap[$item['class']]['menu'][] = array(
        'name'  => $item['name'],
        'color' => $item['color']
    );
}

答案 2 :(得分:0)

尝试减少循环次数(2)

$all = [];

function getSelectClassData(array &$all)
{
    $finalArr = [];

    while (count($all) > 1) {
        $res             = [];
        $class           = array_values($all)[0]->class;
        $selectedDataArr = getSelectSimilerMenuData($all, $class);
        $res['class']    = $class;
        $res['menu']     = array_values($selectedDataArr);
        $all             = array_diff_key($all,array_flip(array_keys($selectedDataArr)));
        $finalArr[]      = $res;
    }

    return $finalArr;
}

function getSelectSimilerMenuData(array $all, $class) 
{
    return array_filter(
        $all,
        function ($e) use ($class) {
            return $e->class == $class;
        }
    );
}

print_r(getSelectClassData($all));