John Thomas
John Thomas

Reputation: 53

PHP Combine arrays inside multidimensional array based on same Value and sort result

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.

Upvotes: 1

Views: 166

Answers (1)

Marc
Marc

Reputation: 3709

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

Upvotes: 1

Related Questions