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
        )

)
  • 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. – Rizier123 Mar 02 '16 at 04:08
  • Why dont you make it the key itself – haseeb Mar 02 '16 at 04: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](https://eval.in/528472). It also introduces the possibility that I may have misunderstood your data structure, and give you an incorrect answer! – scrowler Mar 02 '16 at 04: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.) – Rizier123 Mar 02 '16 at 04:31
  • @Rizier123 I deleted it because I had misunderstood the desired output - thanks for the comments though :-) – scrowler Mar 02 '16 at 05:21

2 Answers2

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

scrowler
  • 24,273
  • 9
  • 60
  • 92
-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 );`
Justin Howard
  • 5,504
  • 1
  • 21
  • 48
channasmcs
  • 1,104
  • 12
  • 27
  • 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 – scrowler Mar 02 '16 at 04:29
  • so then can use this only $result = array_merge( $array1, $array2 ); – channasmcs Mar 02 '16 at 07:02