4

I have this result from a foreach loop. I tried looping through the array using foreach from the answers in StackOverflow, but I'm having trouble when doing it under another foreach loop.

Array
(
    [0] => Array
        (
            [referenceUid] => FF875951-87CB-942F-84A2-46C620BF07C8
            [itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
            [total] => 300.00
        )

)

Array
(
    [0] => Array
        (
            [referenceUid] => D267A795-E142-C25B-E042-D8A519B57DF4
            [itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
            [total] => 150.00
        )

)
Array
(
    [0] => Array
        (
            [referenceUid] => D267A795-E142-C25B-E042-D8A519B57DF4
            [itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
            [total] => 150.00
        )

)

What I want is to merge the array with duplicate values on referenceUid column. Something like this:

Array
(
    [0] => Array
        (
            [referenceUid] => FF875951-87CB-942F-84A2-46C620BF07C8
            [itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
            [total] => 300.00
        )

)

Array
(
    [0] => Array
        (
            [referenceUid] => D267A795-E142-C25B-E042-D8A519B57DF4
            [itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
            [total] => 150.00
        )

    [1] => Array
        (
            [referenceUid] => D267A795-E142-C25B-E042-D8A519B57DF4
            [itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
            [total] => 150.00
        )

)
5
  • If I get your question right, you just want to group all arrays together where the Uid is the same right? If yes where are you stuck? Show your current code/attempt to solve the problem. Commented Mar 2, 2016 at 4:08
  • Why dont you make it the key itself Commented Mar 2, 2016 at 4:08
  • For future reference it's a good idea to provide PHP code arrays as example input so we can easily reproduce your data structure instead of having to hack it all up and re-assemble it like this. It also introduces the possibility that I may have misunderstood your data structure, and give you an incorrect answer! Commented Mar 2, 2016 at 4:20
  • @RobbieAverill To your answer: 1) Good explanation what you actually do here! 2) If you look at OP's output again you see that his values are wrapped in another array, e.g. $values['referenceUid']; should probably be $values[0]['referenceUid']; 3) Not sure if he wants to just add the "total" together or the entire array 4) Wouldn't put explanation in code, since it make it less readable (No idea why you deleted the answer.) Commented Mar 2, 2016 at 4:31
  • @Rizier123 I deleted it because I had misunderstood the desired output - thanks for the comments though :-) Commented Mar 2, 2016 at 5:21

2 Answers 2

7

You can construct a new (merged) array and loop your input to assemble the new structure.

An important consideration is to use the common key (referenceUid) as the array key in your new array so you can reference it easily. If you don't want it at the end, simply reset the array keys e.g. $out = array_values($out).

Here's an example:

$output = array();

foreach ($input as $values) {
    $key = $values['referenceUid'];
    $output[$key][] = $values;
}

// Don't want the referenceUid in the keys? Reset them:
$output = array_values($output);

Example

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

Comments

-3
//both arrays will be merged including duplicates
$result = array_merge( $array1, $array2 );
//duplicate objects will be removed
$result = array_map("unserialize", array_unique(array_map("serialize", $result)));
//array is sorted on the bases of id
sort( $result );`

2 Comments

1. Without the splat operator this would become unmanageable with lots of arrays and 2. The arrays/objects are not duplicates because the values need to be combined
so then can use this only $result = array_merge( $array1, $array2 );

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.