1

I have one array and try to change some key and value for example if sku are same than i need to merge image. Below array i have

    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
            )
)

I need to merge array like this

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
        )
)

If any php function is there than please let me know or any code suggestion

5

2 Answers 2

1

Using simple 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>';

?>
Sign up to request clarification or add additional context in comments.

Comments

1

This could work for you:

$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>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.