1

I am trying to merge an array containing a multidimensional array using the value inside the multidimensional array called [color_id] key and combining the values inside the [details] key, Then, sort the values in the [model] key from least to greatest.

Here is the multidimensional array:

Array (
[0] => Array
    (
        [color_id] => 5
        [details] => Array
            (
                [0] => Array
                    (
                        [quantity] => 1
                        [model] => 387110
                        [size] => small
                    )

                [1] => Array
                    (
                        [quantity] => 1
                        [model] => 771410
                        [size] => medium
                    )
    )
[1] => Array
    (
        [color_id] => 5
        [details] => Array
            (
                [0] => Array
                    (
                        [quantity] => 1
                        [model] => 811110
                        [size] => medium
                    )

                [1] => Array
                    (
                        [quantity] => 1
                        [model] => 958010
                        [size] => large
                    )
            )
    )

[2] => Array
    (
        [color_id] => 36
        [details] => Array
            (
                [0] => Array
                    (
                        [quantity] => 1
                        [model] => 811310
                        [size] => small
                    )

                [1] => Array
                    (
                        [quantity] => 1
                        [model] => 101901
                        [size] => large
                    )
            )
    )

[3] => Array
    (
        [color_id] => 36
        [details] => Array
            (
                [0] => Array
                    (
                        [quantity] => 1
                        [model] => 387010
                        [size] => medium
                    )
                [1] => Array
                    (
                        [quantity] => 2
                        [model] => 564310
                        [size] => medium
                    )
                [2] => Array
                    (
                        [quantity] => 1
                        [model] => 864328
                        [size] => small
                    )
            )

    ) 
[4] => Array
    (
        [color_id] => 74
        [details] => Array
            (
                [0] => Array
                    (
                        [quantity] => 1
                        [model] => 469871
                        [size] => large
                    )
                [1] => Array
                    (
                        [quantity] => 2
                        [model] => 368599
                        [size] => medium
                    )
                [2] => Array
                    (
                        [quantity] => 1
                        [model] => 785958
                        [size] => small
                    )
            )
    ) 
[5] => Array
    (
        [color_id] => 74
        [details] => Array
            (
                [0] => Array
                    (
                        [quantity] => 1
                        [model] => 958741
                        [size] => small
                    )
                [1] => Array
                    (
                        [quantity] => 2
                        [model] => 123688
                        [size] => medium
                    )
            )
    )

)

I'm trying to get this result without any luck.

Array (
[0] => Array
    (
        [color_id] => 5
        [details] => Array
            (
                [0] => Array
                    (
                        [quantity] => 1
                        [model] => 387110
                        [size] => small
                    )

                [1] => Array
                    (
                        [quantity] => 1
                        [model] => 771410
                        [size] => medium
                    )
                [3] => Array
                    (
                        [quantity] => 1
                        [model] => 811110
                        [size] => medium
                    )

                [4] => Array
                    (
                        [quantity] => 1
                        [model] => 958010
                        [size] => large
                    )

    )

[1] => Array
    (
        [color_id] => 36
        [details] => Array
            (
                [0] => Array
                    (
                        [quantity] => 1
                        [model] => 101901
                        [size] => large
                    )
                [1] => Array
                    (
                        [quantity] => 1
                        [model] => 387010
                        [size] => medium
                    )
                [3] => Array
                    (
                        [quantity] => 2
                        [model] => 564310
                        [size] => medium
                    )
                [4] => Array
                    (
                        [quantity] => 1
                        [model] => 811310
                        [size] => small
                    )
                [5] => Array
                    (
                        [quantity] => 1
                        [model] => 864328
                        [size] => small
                    )
            )
    )
[2] => Array
    (
        [color_id] => 74
        [details] => Array
            (
                [0] => Array
                    (
                        [quantity] => 2
                        [model] => 123688
                        [size] => medium
                    )
                [1] => Array
                    (
                        [quantity] => 2
                        [model] => 368599
                        [size] => medium
                    )
                [2] => Array
                    (
                        [quantity] => 1
                        [model] => 469871
                        [size] => large
                    )
                [3] => Array
                    (
                        [quantity] => 1
                        [model] => 785958
                        [size] => small
                    )
                [4] => Array
                    (
                        [quantity] => 1
                        [model] => 958741
                        [size] => small
                    )

            )
    )
)

Greatly appreciate the help. Thank you.

1 Answer 1

1

I would do it like this (expecting your array as $array):

// output array
$newArray = array();

// loop through the $array
for($i=0; $i<count($array); $i++) {
    // check if this is the first iteration
    if($i == 0) {
        // if so, push the first element into the new array
        array_push($newArray,$array[$i]);
        // and continue with the next iteration
        continue;
    }
    // found flag for the color_id key
    $found = false;
    // loop through the new array to check if the color_id is in there yet
    foreach($newArray as &$subArr) {
        // check if there is the color id of the current iteration (of the for loop)
        if($subArr['color_id'] == $array[$i]['color_id']) {
            // if it is, push all the details in this color_id, flag as found and break out
            foreach($array[$i]['details'] as $details) array_push($subArr['details'],$details);
            $found = true;
            break;
        }
    }
    // check if the color_id was found
    if(!$found) {
        // if not, push the current color_id into the new array
        array_push($newArray,$array[$i]);
    }
}

// loop through the new array
foreach($newArray as &$newSubArray) {
    // sort by model
    uasort($newSubArray['details'], function($a,$b){return $a['model']-$b['model'];});
}

By the way: is it necessary to save the color_name in this array? Because it is redundant ;)

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

3 Comments

Thanks. I tried your solution but variable $a and $b are undefined. I have removed the [color_name] key as it is redundant.
Whoops... corrected it - the answer was originally correct - I tried to improve it and made it worse
Thank you. The variables have been defined. However, it looks like it is only pushing the first value of the second, third arrays into the first array, ie. if the second, third arrays have 2 or more values, it only combine the first value into the first array. I have added more keys/values into the example if you would like to check your solution against the new data

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.