1

I have two arrays I want to merge, but leave out elements from the 2nd array that match a value. (id and otherId)

I know how to achieve this with a 2nd loop inside a first loop, but I was wondering if this could be achieved using a single loop, or with a specific php function?

This is an example of what I'm doing now:

foreach ($array1 as $item)
{
    $id = $item['id'];

    foreach ($array2 as $key => $item)
    {
        if ($item['otherId'] == $id)
        {
            unset($array2[$key]);
        }
    }
}

$output = array_merge($array1, $array2);

There will be a lot of elements in both arrays, so I'm looking for the most efficient way of doing this.

Example of arrays:

$array1 = [
    [
        'id' => 1
        'name' => 'item 1'
    ],
    [
        'id' => 2
        'name' => 'item 2'
    ],
    [
        'id' => 3
        'name' => 'item 3'
    ],
    [
        'id' => 4
        'name' => 'item 4'
    ]
];

$array2 = [
    [
        'id' => 1,
        'otherId' => 2,
        'name' => 'item 2'
    ],
    [
        'id' => 2,
        'otherId' => 3,
        'name' => 'item 3'
    ],
    [
        'id' => 3,
        'otherId' => null,
        'name' => 'item 4'
    ],
    [
        'id' => 4,
        'otherId' => null,
        'name' => 'item 5'
    ]
];


$output = [
    [
        'id' => 1
        'name' => 'item 1'
    ],
    [
        'id' => 2
        'name' => 'item 2'
    ],
    [
        'id' => 3
        'name' => 'item 3'
    ],
    [
        'id' => 4
        'name' => 'item 4'
    ],
    [
        'id' => 3,
        'otherId' => null,
        'name' => 'item 4'
    ],
    [
        'id' => 4,
        'otherId' => null,
        'name' => 'item 5'
    ]
];
jlmmns
  • 837
  • 4
  • 14
  • 25
  • _“so I'm looking for the most efficient way of doing this”_ – then please tell us which ways you have found/developed and tried out already. – CBroe Oct 24 '13 at 10:42
  • @CBroe With a loop inside a loop, and check if the values match. But I want to avoid using nested loops. I'll update my question with an example of this. – jlmmns Oct 24 '13 at 10:45
  • Can you post your original code that does the merge – gwillie Oct 24 '13 at 10:51
  • just do array match or array search at first to remove all items from 2nd array that exist in first array and after you've stripped the items out of the second array just do an array merge. – Dave Oct 24 '13 at 10:57
  • @CBroe and gwillie: I edited my post with an example of what I'm doing now. (nested loop) – jlmmns Oct 24 '13 at 11:06
  • @Dave Could you give a light example of this? – jlmmns Oct 24 '13 at 11:07

2 Answers2

2

Do you means this:

<?php

$array1 = array(
    array(
        'id' => 1,
        'name' => 'item 1'
    ),
    array(
        'id' => 2,
        'name' => 'item 2'
    ),
    array(
        'id' => 3,
        'name' => 'item 3'
    ),
    array(
        'id' => 4,
        'name' => 'item 4'
    )
);

$array2 = array(
    array(
        'id' => 1,
        'otherId' => 2,
        'name' => 'item 2'
    ),
    array(
        'id' => 2,
        'otherId' => 3,
        'name' => 'item 3'
    ),
    array(
        'id' => 3,
        'otherId' => null,
        'name' => 'item 4'
    ),
    array(
        'id' => 4,
        'otherId' => null,
        'name' => 'item 5'
    )
);

$output = $array1;
for($i=0,$c=sizeof($array2);$i<$c;$i++){
  if(is_null($array2[$i]['otherId']))
      array_push($output, $array2[$i]);
}

var_dump($output);

Live example see here

Also you can do array_push() directly into $array1 to reduce memory usage. Example here

dododo
  • 256
  • 1
  • 6
0

How about this function:

function searchForId($id, $array) {
foreach ($array as $key => $val) {
   if ($val['uid'] === $id) {
       return $key;
    }
   }
   return null;
}

You can use it to search array. To avoid any confusion, I'm just paste this to make it easier to see the solution. The complete solution can be found at https://stackoverflow.com/a/6661561/1238227 thanks to @Jakub Truneček

Community
  • 1
  • 1
Marko Vasic
  • 191
  • 2
  • 3
  • 13