0

I am trying to merge 2 arrays in PHP

let's take this example

$array_old = array (
  'product_title' => 'LG Nexus 4 Android Smartphone 16GB Sim-Free',
  'site' => 'http://www.amazon.co.uk/LG-Nexus-Android-...',
  'title' => 'LG Nexus 4 Android Smartphone 16GB Sim-Free',
  'price' => '300.00',
  'delivery' => '0.00',
   'codes' => 
  array (
    'mpn' => 'LG-E960',
  ),
)


$array_new = array (
  'product_title' => 'Google Nexus 4 (UK 16GB Black)',
  'price' => '319.99',
  'brand' => 'Google Nexus 4 (UK, 16GB, Black)',
  'attributes' => 'color: black',
  'codes' => 
  array (
    'SKU' => '239049',
    'mpn' => 'E960'
  ),
)

After merging I need to have:

$array_old = array (
  'product_title' => 'LG Nexus 4 Android Smartphone 16GB Sim-Free',
  'site' => 'http://www.amazon.co.uk/LG-Nexus-Android-...',
  'title' => 'LG Nexus 4 Android Smartphone 16GB Sim-Free',
  'price' => '300.00',
  'delivery' => '0.00',
  'attributes' => 'color: black',
    'codes' => 
  array (
    'mpn' => 'LG-E960',
    'SKU' => '239049',
  ),
)

What I need is to avoid rewriting the field, I need to keep all the values from the first array and just add the values from the second one. Remember that I have an array inside the array.

Now I am trying this way (array_merge way) in a small Codeigniter framework app:

if((isset($array_new['codes']))&&(isset($array_old['codes']))) $array_merged['codes'] = array_merge($array_new['codes'],$array_old['codes']);
elseif(isset($array_new['codes'])) $array_merged['codes'] = $array_new['codes'];
elseif((isset($scraper_content['codes']))) $array_merged['codes'] = $array_old['codes'];
if(isset($array_new)) $array_old = array_merge($array_new,$array_old);
if (isset($array_merged['codes'])) $array_old['codes'] = $array_merged['codes'];

Do you know a better way to do this? The tricky thing is the second level array, the array inside the array.

PS: I haven't find a good response to this question, sorry If I miss something or if the message is not clear, any questions are welcome

Cheers

Victor Spinei
  • 35
  • 1
  • 12
  • 1
    [`array_merge_recursive()`](http://www.php.net/array_merge_recursive)? – nickb Jun 11 '13 at 17:52
  • a combination of `array_merge_recursive()` and the array union operator, +, might help `$array_merged = $array_new + $array_old;` http://www.php.net/manual/en/function.array-merge.php#example-4849 – pulsar Jun 11 '13 at 17:59
  • Interesting approach, thanks for suggestion – Victor Spinei Jun 12 '13 at 08:48

1 Answers1

1

Try with double array_merge:

function merge_products($a, $b) {
    $merged = array_merge($b, $a);
    $merged['codes'] = array_merge($b['codes'], $a['codes']);

    return $merged;
}

http://codepad.org/gY29h814

Matteo Manchi
  • 344
  • 4
  • 11
  • all excellent except some time we have empty arrays. Eg: $b['codes'] is empty – Victor Spinei Jun 12 '13 at 08:45
  • if $a['codes'] is not there, array_merge($b['codes'], $a['codes']) will fail. if is empty is just fine. maybe I better change my script to have the $a['codes'] empty and not missing. if I would have the credit man, I would give you my vote. Thank you Matteo – Victor Spinei Jun 13 '13 at 16:02