Array merge based on key
Posted: Wed Dec 01, 2010 9:44 am
hi,
I've got two php arrays
The first array represents the total distance covered by a car, and I call it
$a
Array ( [0] => stdClass Object ( [total_distance] => 1000 [car_id] => 1 ) [1] => stdClass Object ( [total_distance] => 500 [car_id] => 2 ) )
The second array, is the array of cars, and I call it
$b
Array ( [0] => stdClass Object ([car_id] => 1 [year] => 2008 ) [1] => stdClass Object ( [car_id] => 2 [year] => 2010 ) )
Is it possible to merge these arrays, provided they have the car_id key in common?
The resulting array should be:
$c
Array ( [0] => stdClass Object ([car_id] => 1 [year] => 2008 [total_distance] => 1000 ) [1] => stdClass Object ( [car_id] => 2 [year] => 2010 [total_distance] => 500 ) )
I've got two php arrays
The first array represents the total distance covered by a car, and I call it
$a
Array ( [0] => stdClass Object ( [total_distance] => 1000 [car_id] => 1 ) [1] => stdClass Object ( [total_distance] => 500 [car_id] => 2 ) )
The second array, is the array of cars, and I call it
$b
Array ( [0] => stdClass Object ([car_id] => 1 [year] => 2008 ) [1] => stdClass Object ( [car_id] => 2 [year] => 2010 ) )
Is it possible to merge these arrays, provided they have the car_id key in common?
The resulting array should be:
$c
Array ( [0] => stdClass Object ([car_id] => 1 [year] => 2008 [total_distance] => 1000 ) [1] => stdClass Object ( [car_id] => 2 [year] => 2010 [total_distance] => 500 ) )