0

I've been trying to merge this array ONLY WHERE $array[4] exists more than one time, example: $array[4] == 'red' exactly twice. How can I merge only those arrays while keeping the others? I have made several attempts at this and I am willing to include my efforts if asked.

Consider this array:

array(3) {
  [0]=>
     array(5) {
               [0]=>
                 string(3) "UID"
               [1]=>
                 string(3) "532"
               [2]=>
                 string(1) "2"
               [3]=>
                 string(9) "Domain(s)"
               [4]=>
                 string(20) "red"
             }
 [1]=>
    array(5) {
             [0]=>
                string(3) "UID"
             [1]=>
                string(3) "532"
             [2]=>
                string(7) "License"
             [3]=>
                string(3) "Fee"
             [4]=>
                string(20) "red"
            }
 [2]=>
     array(5) {
               [0]=>
                 string(3) "UID"
               [1]=>
                 string(3) "536"
               [2]=>
                 string(7) "License"
               [3]=>
                 string(3) "Fee"
               [4]=>
                 string(16) " University Test"
             }
    }

TRYING TO ACHIEVE:

    array(3) {
  [0]=>
     array(5) {
               [0]=>
                 string(3) "UID"
               [1]=>
                 string(3) "532"
               [2]=>
                 string(1) "2"
               [3]=>
                 string(9) "Domain(s)"
               [4]=>
                 string(20) " red"
               [5]=>
                 string(3) "Fee"
               [6]=>
                 string(7) "License"
             }
 [1]=>
     array(5) {
               [0]=>
                 string(3) "UID"
               [1]=>
                 string(3) "536"
               [2]=>
                 string(7) "License"
               [3]=>
                 string(3) "Fee"
               [4]=>
                 string(16) " University Test"
             }
    }
3
  • Is there any approach you've tried? Commented Aug 14, 2015 at 21:28
  • You'd get the key of the duplicate then would remove it using the key. You'd be left with a gap in the key but you'd use array_values to reorganize the keys. Commented Aug 14, 2015 at 21:28
  • Does the order of elements in the resulting array matter? Commented Aug 14, 2015 at 21:39

1 Answer 1

1
foreach ($test as $item) {
    if (!isset($merged[$item[4]])) {
        // add the item to the merged array using key=$item[4]
        $merged[$item[4]] = $item;
    } else {
         // merge the item with the item that is already in the array at key=$item[4]
         $merged[$item[4]] = array_unique(array_merge($merged[$item[4]], $item));
         // array_unique is necessary because array_merge will not overwrite numeric keys
    }
}
// convert the keys back to numeric (if you care to)
$merged = array_values($merged);
Sign up to request clarification or add additional context in comments.

1 Comment

This. Worked. Thanks mate. Didn't know you could array_unique(array_merge))!

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.