Jump to content

Combining arrays


blinks58

Recommended Posts

I have 2 arrays :

 

$array1=

Array (
 [0] => Array (
  [id] => 411
  [pa] => book
  [rm] => 32)
 [1] => Array (
  [id] => 974
  [pa] => journal
  [rm] => 76)
 [2] => Array (
  [id] => 3041
  [pa] => book
  [rm] => 85 )
 [3] => Array (
  [id] => 3852
  [pa] => paper
  [rm] => 60 )
  )
  
$array2=

Array (
 [0] => Array (
  [AU] => 3041
  [NL] => 1490240
  [CID] => 52
 [1] => Array (
  [AU] => 974
  [NL] => 1490241
  [CID] => 76
 [2] => Array (
  [AU] => 390
  [NL] => 1491023
  [CID] => 56 )
  )

 

where the id field in $array1 should match the AU field in $array2 (although there won't always be a match in $array2).

 

I want to combine the 2 arrays into 1, where all the rows in $array1 are retained, along with any fields in $array2 that are not in $array1. Like this -

 

Array (
 [0] => Array (
  [id] => 411
  [pa] => book
  [rm] => 32)
  [NL] => 
  [CID] =>  )
 [1] => Array (
  [id] => 974
  [pa] => journal
  [rm] => 76)
  [NL] => 1490241
  [CID] => 76 )  
 [2] => Array (
  [id] => 3041
  [pa] => book
  [rm] => 85
  [NL] => 1490240
  [CID] => 52 )  
 [3] => Array (
  [id] => 3852
  [pa] => paper
  [rm] => 60
  [NL] => 
  [CID] =>  )
  )   

 

I'd appreciate some assistance about how best to do this. I've been using various combinations of in_array so far, with no luck.

 

Link to comment
https://forums.phpfreaks.com/topic/278455-combining-arrays/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.