在Key上递归合并数组数组

时间:2017-04-19 15:11:39

标签: php arrays multidimensional-array merge

我想将匹配makes的数组与匹配的models数组合并但是知道从哪里开始。我玩array_merge_recursive,但输出不是我想要的。示例输出:

Array
(
[0] => Array
    (
        [make] => Chevrolet
        [model] => Silverado 1500
    )
)
)

预期输出:

Array
(
[0] => Array
    (
        [make] => Chevrolet
        [model] => Array
        (
            [0] => Silverado 1500
            [1] => Malibu
            [2] => Equinox
            [3] => Camaro
        )
    )

[1] => Array
    (
        [make] => Cadillac
        [model] => XT5
    )

[2] => Array
    (
        [make] => GMC
        [model] => Terrain
    )
)

输入:

Array
(
[0] => Array
    (
        [make] => Chevrolet
        [model] => Silverado 1500
    )

[1] => Array
    (
        [make] => Cadillac
        [model] => XT5
    )

[2] => Array
    (
        [make] => Chevrolet
        [model] => Malibu
    )

[3] => Array
    (
        [make] => Chevrolet
        [model] => Equinox
    )

[4] => Array
    (
        [make] => GMC
        [model] => Terrain
    )

[5] => Array
    (
        [make] => Chevrolet
        [model] => Camaro
    )
[6] => Array
    (
        [make] => Chevrolet
        [model] => Silverado 1500
    )
)

4 个答案:

答案 0 :(得分:2)

您可以使用array_count_valuesarray_splice来实现此目的,事先使用array_multisort'make'排序数组:

$makes = array_column($array, 'make');
array_multisort($array, $makes);
$makes = array_count_values($makes);
$models = array_column($array, 'model');

$result = [];
foreach ($makes as $make => $count) {
    $_models = array_splice($models, 0, $count);
    $result[] = [
        'make' => $make,
        'model' => $count === 1 ? $_models[0] : array_unique($_models)
    ];
}

应用array_unique可确保模型中没有重复项。

这是working demo

答案 1 :(得分:1)

Try this code snippet here

<?php

ini_set('display_errors', 1);

$array = array(
    0 => Array
        (
        "make" => "Chevrolet",
        "model" => "Silverado 1500",
    ),
    1 => Array
        (
        "make" => "Cadillac",
        "model" => "XT5",
    ),
    2 => Array
        (
        "make" => "Chevrolet",
        "model" => "Malibu",
    ),
    3 => Array
        (
        "make" => "Chevrolet",
        "model" => "Equinox",
    ),
    4 => Array
        (
        "make" => "GMC",
        "model" => "Terrain",
    ),
    5 => Array
        (
        "make" => "Chevrolet",
        "model" => "Camaro",
    ),
    6 => Array
        (
        "make" => "Chevrolet",
        "model" => "Silverado 1500",
    ),
);
$result=array();
foreach($array as $key => $value)
{
    if(!isset($result[$value["make"]]))
    {
        $result[$value["make"]]=$value;
    }
    else
    {
        if(is_string($result[$value["make"]]["model"]))
        {
            //if current value is string then restoring previous values in array
            $temp=$result[$value["make"]]["model"];
            $result[$value["make"]]["model"]=array();
            $result[$value["make"]]["model"][]=$temp;
        }
        $result[$value["make"]]["model"][]=$value["model"];//adding values to array
    }
}
print_r($result);

<强>输出:

Array
(
    [Chevrolet] => Array
        (
            [make] => Chevrolet
            [model] => Array
                (
                    [0] => Silverado 1500
                    [1] => Malibu
                    [2] => Equinox
                    [3] => Camaro
                    [4] => Silverado 1500
                )

        )

    [Cadillac] => Array
        (
            [make] => Cadillac
            [model] => XT5
        )

    [GMC] => Array
        (
            [make] => GMC
            [model] => Terrain
        )

)

答案 2 :(得分:0)

罗马的一种方式......

 //$yourArray;
 $newArray=array();
 array_map(function($v) use (&$newArray){
    $newArray[$v['make']]['make']=$v['make'];
    $newArray[$v['make']]['model'][]=$v['model'];
 },$yourArray);
 $newArray = array_values($newArray);
 print_r($newArray);

注意:相同的子数组元素应始终具有相同的类型(此处为数组),因此在此处不要将单个数组条目更改为字符串。从后来的代码中可以更容易处理(No if(is_array()){}else{}我从simpleXML行为中吸取了教训;-))。希望你知道如果真正需要的话,如何制作一个字符串。

度过愉快的一天。

答案 3 :(得分:0)

您可以使用foreach来获取它。的 Live demo

foreach($array as $v)
{
  $arr[$v['make']]['make'] = $v['make'];
  $arr[$v['make']]['model'][] = $v['model'];
}
$result = array_map(function($v){
if(count($v['model']) == 1)
  $v['model'] = $v['model'][0];
return $v;
}, array_values($arr));

print_r($result);