I wrote the following code to take arrays in PHP and "join" them together the way that a LEFT JOIN would in MySQL. For my sake I wrote the function using foreach and passed the arrays in and returned a new array... I feel like it's pretty readable, but I also know that it's inefficient.
Ideally, I'd like this function to handle upwards of 10,000 rows per array, and I suspect that means: 1) passing $original by reference (to save memory), and 2) using one of PHP iterative array function rather than a foreach (to save processing time).
Usually I don't post these sort of "quiz" like questions, but I feel like the answer to this will benefit the community. (Like this guy: Join 2 multidimensional array)
May the 1) fastest, and 2) lowest memory consuming answer win! :P
<?php
// Join Arrays on Keys (**updated with knittl's suggestion**)
function array_join($original, $merge, $on) {
if (!is_array($on)) $on = array($on);
foreach ($merge as $remove => $right) {
foreach ($original as $index => $left) {
foreach ($on as $from_key => $to_key) {
if (!isset($original[$index][$from_key])
|| !isset($right[$to_key])
|| $original[$index][$from_key] != $right[$to_key])
continue 2;
}
$original[$index] = array_merge($left, $right);
unset($merge[$remove]);
}
}
return array_merge($original, $merge);
}
// Test Arrays
$data1 = array(
array(
'productId' => '822335',
'dateHour' => '2011-11-17 06:00:00',
'qtySold' => '200',
'qtyCanceled' => '10',
),
array(
'productId' => '822335',
'dateHour' => '2011-11-17 07:00:00',
'qtySold' => '100',
'qtyCanceled' => '20',
),
array(
'productId' => '822336',
'dateHour' => '2011-11-17 06:00:00',
'qtySold' => '0',
'qtyCanceled' => '30',
),
array(
'productId' => '822336',
'dateHour' => '2011-11-17 07:00:00',
'qtySold' => '50',
'qtyCanceled' => '40',
),
);
$data2 = array(
array(
'entity_id' => '822335',
'dateHour' => '2011-11-17 06:00:00',
'productInventory' => '300',
),
array(
'entity_id' => '822335',
'dateHour' => '2011-11-17 07:00:00',
'productInventory' => '200',
),
array(
'entity_id' => '822336',
'dateHour' => '2011-11-17 06:00:00',
'productInventory' => '100',
),
array(
'entity_id' => '822336',
'dateHour' => '2011-11-17 07:00:00',
'productInventory' => '50',
),
);
// Usage
$result = array_join($data1, $data2, array(
'productId' => 'entity_id',
'dateHour' => 'dateHour'
));
print_r($result);