PHP数组合并基于key&值

时间:2016-12-29 03:49:18

标签: php arrays multidimensional-array

我有一个数组,并尝试更改某些键和值,例如,如果 sku 与我需要合并图像相同。数组下面有

    Array
    (
        [0] => Array
            (
                [sku] => h-eldora
                [name] => H ELDORA
                [image] => s/files/1/1282/4221/products/h_eldora_01.jpg?v=1476667054
            )

        [1] => Array
            (
                [sku] => h-eldora
                [name] => 
                [image] => s/files/1/1282/4221/products/h_eldora_02.jpg?v=1475116221
            )

        [2] => Array
            (
                [sku] => h-eldora
                [name] => 
                [image] => s/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598
            )

        [3] => Array
            (
                [sku] => hl-dracy
                [name] => HL DRACY
                [image] => s/files/1/1282/4221/products/h_dracy_01.jpg?v=1475115222
            )

        [4] => Array
            (
                [sku] => hl-dracy
                [name] => 
                [image] => s/files/1/1282/4221/products/h_dracy_02.jpg?v=1475115223
            )

        [5] => Array
            (
                [sku] => hl-dracy
                [name] => 
                [image] =>s/files/1/1282/4221/products/20100707164858197_1_633237aa-36ec-441b-a074-03844f6a0858.jpg?v=1475719793
            )
)

我需要像这样合并数组

Array
(


    [0] => Array
        (
            [sku] => h-eldora
            [name] => 
            [image1] => s/files/1/1282/4221/products/h_eldora_02.jpg?v=1475116221
            [image2] => s/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598
            [image3] => s/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598
         )
          [1] => Array
        (
            [sku] => hl-dracy
            [name] => HL DRACY
            [image1] => s/files/1/1282/4221/products/h_dracy_01.jpg?v=1475115222
            [image2] => s/files/1/1282/4221/products/h_dracy_02.jpg?v=1475115223
            [image3] => s/files/1/1282/4221/products/20100707164858197_1_633237aa-36ec-441b-a074-03844f6a0858.jpg?v=1475719793
        )
)

如果有任何PHP功能,请告诉我或任何代码建议

2 个答案:

答案 0 :(得分:1)

使用简单的PHP:

<?php

$arr1 = array(
    0 => array(
        'sku' => 'h-eldora',
        'name' => 'H ELDORA',
        'image' => 's/files/1/1282/4221/products/h_eldora_01.jpg?v=1476667054'
    ),
    1 => array(
        'sku' => 'h-eldora',
        'name' => '',
        'image' => 's/files/1/1282/4221/products/h_eldora_02.jpg?v=1475116221'
    ),
    2 => array(
        'sku' => 'h-eldora',
        'name' => '',
        'image' => 's/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598'
    ),
    3 => array(
        'sku' => 'hl-dracy',
        'name' => 'HL DRACY',
        'image' => 's/files/1/1282/4221/products/h_dracy_01.jpg?v=1475115222'
    ),
    4 => array(
        'sku' => 'hl-dracy',
        'name' => 'H ELDORA',
        'image' => 's/files/1/1282/4221/products/h_dracy_02.jpg?v=1475115223'
    ),
    5 => array(
        'sku' => 'hl-dracy',
        'name' => 'H ELDORA',
        'image' => 's/files/1/1282/4221/products/20100707164858197_1_633237aa-36ec-441b-a074-03844f6a0858.jpg?v=1475719793'
    )
);

$newArr = $imgIndex = array();
foreach($arr1 as $a){
    if( !array_key_exists($a['sku'],$newArr) ){
        $newArr[$a['sku']] = array(
            'sku' => $a['sku'],
            'name' => $a['name'],
            'image1' => $a['image']
        );
        $imgFound[$a['sku']] = 1;
    }else{
        $imgFound[$a['sku']]++;
        $newArr[$a['sku']]['image'.$imgFound[$a['sku']]] = $a['image'];
    }
}

unset($imgFound);
echo '<pre>'; print_r($newArr); echo '</pre>';

?>

答案 1 :(得分:1)

这可能对您有用:

$data = // your input array

$uniqueSKUs = Array();
$newArray = Array();
$currentIndex = -1;

foreach ($data as $item) {
    if (!in_array($item['sku'], $uniqueSKUs)) {
        $currentIndex++;

        $uniqueSKUs[] = $item['sku'];

        $newArray[$currentIndex] = Array(
            'sku' => $item['sku'],
            'name' => $item['name']
        );
    }

    $newArray[$currentIndex]['images'][] = $item['image'];
}

echo "<pre>";
var_dump($newArray);
echo "</pre>";