Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unfortunately, because of the input data format, I can't see any way to do this that doesn't involve an <a href="http://en.wikipedia.org/wiki/Big_O_notation" rel="nofollow" title="n = number of elements in $plusMinus, m = number of elements in $filler, p is variable dependent on the order of the $temp product array but it is at least the size of $temp, see Quicksort for more info on p"><code>O(n + m + p)</code></a> operation. But no matter, you gotta do what you gotta do.</p> <p>Firstly I would start by filtering the useless elements from the <code>PlusMinus</code> array. Since it's already fairly close to the desired output format, it makes sense to use this as the base of the result.</p> <pre><code>$temp = array(); foreach ($plusMinus as $item) { if (!empty($item['date'])) { $temp[$item['date']] = $item; } } </code></pre> <p>Notice that I used the <code>date</code> as the index of the temporary array we're using to build the result. This is to allow you to easily ensure that the result array is in the correct order, and to quickly check whether an item needs to be added from the <code>Filler</code> array.</p> <p>Next, we need to add any missing elements from the <code>Filler</code> array:</p> <pre><code>foreach ($filler as $item) { if (!isset($temp[$item[0]])) { $temp[$item[0]] = array( 'date' =&gt; $item[0], 'plusMinus' =&gt; 0 ); } } </code></pre> <p>Now all the data is in the array in the correct format, we just need to <a href="http://php.net/ksort" rel="nofollow">sort</a> it:</p> <pre><code>ksort($temp); </code></pre> <p>...and get convert it back to an indexed array:</p> <pre><code>return array_values($temp); </code></pre> <p>No need for the performance killing nested loops or complex flow control.</p> <p><a href="http://codepad.viper-7.com/nVizfW" rel="nofollow">See it working</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    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