Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I optimize my array_join (simulates a LEFT JOIN) function?
    primarykey
    data
    text
    <p>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.</p> <p>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).</p> <p>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: <a href="https://stackoverflow.com/questions/8833522/join-2-multidimensional-array">Join 2 multidimensional array</a>)</p> <p>May the 1) fastest, and 2) lowest memory consuming answer win! :P</p> <pre><code>&lt;?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 =&gt; $right) { foreach ($original as $index =&gt; $left) { foreach ($on as $from_key =&gt; $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' =&gt; '822335', 'dateHour' =&gt; '2011-11-17 06:00:00', 'qtySold' =&gt; '200', 'qtyCanceled' =&gt; '10', ), array( 'productId' =&gt; '822335', 'dateHour' =&gt; '2011-11-17 07:00:00', 'qtySold' =&gt; '100', 'qtyCanceled' =&gt; '20', ), array( 'productId' =&gt; '822336', 'dateHour' =&gt; '2011-11-17 06:00:00', 'qtySold' =&gt; '0', 'qtyCanceled' =&gt; '30', ), array( 'productId' =&gt; '822336', 'dateHour' =&gt; '2011-11-17 07:00:00', 'qtySold' =&gt; '50', 'qtyCanceled' =&gt; '40', ), ); $data2 = array( array( 'entity_id' =&gt; '822335', 'dateHour' =&gt; '2011-11-17 06:00:00', 'productInventory' =&gt; '300', ), array( 'entity_id' =&gt; '822335', 'dateHour' =&gt; '2011-11-17 07:00:00', 'productInventory' =&gt; '200', ), array( 'entity_id' =&gt; '822336', 'dateHour' =&gt; '2011-11-17 06:00:00', 'productInventory' =&gt; '100', ), array( 'entity_id' =&gt; '822336', 'dateHour' =&gt; '2011-11-17 07:00:00', 'productInventory' =&gt; '50', ), ); // Usage $result = array_join($data1, $data2, array( 'productId' =&gt; 'entity_id', 'dateHour' =&gt; 'dateHour' )); print_r($result); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload