如果某些键/值在数组中匹配,则合并组合数组

时间:2015-04-01 16:56:29

标签: php arrays

让我们说我有一个看起来像这样的数组:

Array
(
    [0] => Array
        (
            [id] => 44091
            [epid] => 109912002
            [makes] => Honda
            [models] => Civic
            [years] => 2000
            [trims] => All
            [engines] => 1.6L 1590CC 97Cu. In. l4 GAS SOHC Naturally Aspirated
            [notes] => 
        )

    [1] => Array
        (
            [id] => 77532
            [epid] => 83253884
            [makes] => Honda
            [models] => Civic
            [years] => 2000
            [trims] => All
            [engines] => 1.6L 1595CC l4 GAS DOHC Naturally Aspirated
            [notes] => 
        )

    [2] => Array
        (
            [id] => 151086
            [epid] => 109956658
            [makes] => Honda
            [models] => Civic
            [years] => 1999
            [trims] => All
            [engines] => 1.6L 1590CC 97Cu. In. l4 GAS SOHC Naturally Aspirated
            [notes] => 
        )
)

如果特定的键/值对匹配,我想以某种方式合并/分组/组合你所称的任何内容。

所以我的条件是:

如果制作&型号&岁月修剪是相同的,组合成1个数组。其他键/值(如id / epid / trims / engines / notes)不相关,如果可能,可以使用/继承其中1个匹配的条目。

一旦可能,我想添加另一个条件来查找:

如果制作&型号&岁月修剪和修剪发动机组合成1阵列。

也许我对自己感到困惑,两者都可以使用相同的代码。

无论如何,在这种情况下,我希望结果看起来像这样:

Array
(
    [0] => Array
        (
            [id] => 44091
            [epid] => 109912002
            [makes] => Honda
            [models] => Civic
            [years] => 2000
            [trims] => All
            [engines] => 1.6L 1590CC 97Cu. In. l4 GAS SOHC Naturally Aspirated
            [notes] => 
        )

    [1] => Array
        (
            [id] => 151086
            [epid] => 109956658
            [makes] => Honda
            [models] => Civic
            [years] => 1999
            [trims] => All
            [engines] => 1.6L 1590CC 97Cu. In. l4 GAS SOHC Naturally Aspirated
            [notes] => 
        )
)

请注意,1999年的数组未合并。

我试过搞乱array_unique,array_flip但是无法让它工作。

如果重要的话,我使用的是PHP 5.6.7。

希望有人知道我在说什么。

感谢。

2 个答案:

答案 0 :(得分:1)

这可能会有所帮助

echo '<pre>';
foreach($name_of_your_array as $k=>$v){
    $sorted_array["$v[makes]$v[models]$v[years]$v[trims]"]=$v;
}
$sorted_array=array_values($sorted_array);
print_r($sorted_array);

输出

   Array(
    [0] => Array
        (
            [id] => 77532
            [epid] => 83253884
            [makes] => Honda
            [models] => Civic
            [years] => 2000
            [trims] => All
            [engines] => 1.6L 1595CC l4 GAS DOHC Naturally Aspirated
            [notes] => 
        )

    [1] => Array
        (
            [id] => 151086
            [epid] => 109956658
            [makes] => Honda
            [models] => Civic
            [years] => 1999
            [trims] => All
            [engines] => 1.6L 1590CC 97Cu. In. l4 GAS SOHC Naturally Aspirated
            [notes] => 
        )

)

答案 1 :(得分:0)

使用Being Sunny对此链接的建议:

php filter array values and remove duplicates from multi dimensional array

我能够修改它,因为那只是针对单个键/值而且现在正在使用它:

// Create dummy array for checking duplicates
$taken = array();

// Loop through each item and if doesn't exist add to taken array. If exist then unset the key.
foreach($comps as $key => $item) {

    $string = $item['makes'] . $item['models'] . $item['years'] . $item['trims'] . $item['engines'];

    if(!in_array($string, $taken)) {

        $taken[] = $string;

    } else {

        unset($comps[$key]);

    }

}

// Reindex the array
$comps = array_values($comps);