Note that there are some explanatory texts on larger screens.

plurals
  1. POCombination of arrays
    primarykey
    data
    text
    <p>I have tried to get the below code to work for a good couple of hours, but just don't succeed.</p> <p>I have this date array: </p> <pre><code>Array ( [0] =&gt; Array ( [0] =&gt; 2007 ) [1] =&gt; Array ( [0] =&gt; 2008 ) [2] =&gt; Array ( [0] =&gt; 2009 ) ... ) </code></pre> <p>and this plusMinus one:</p> <pre><code>Array ( [0] =&gt; Array ( [plus] =&gt; 2 [date] =&gt; 2007 ) [1] =&gt; Array ( [minus] =&gt; 1 [date] =&gt; 2008 ) [2] =&gt; Array ( [minus] =&gt; 1 [date] =&gt; ) [3] =&gt; Array ( [plus] =&gt; 1 [date] =&gt; 2010 [minus] =&gt; 1 ) ) </code></pre> <p>I have been trying to combine them into this:</p> <pre><code>Array ( [0] =&gt; Array ( [date] =&gt; 2007 [plus]=&gt; 2) [1] =&gt; Array ( [date] =&gt; 2008 [minus]=&gt; 1) [2] =&gt; Array ( [date] =&gt; 2009 [plusMinus]=&gt; 0) [3] =&gt; Array ( [date] =&gt; 2010 [plus] =&gt; 1 [minus]=&gt;1 ) ... ) </code></pre> <p>So basically I want to check if a value of the date array exists in the plusMinus array. If true the date and values from the plusMinus array shall replace the entry in the date array. If false, the original date array entry is complemented by a [plusMinus] => 0 key-value pair.</p> <p>The way I have tried to do it is this:</p> <pre><code>foreach ($filler as $key1 =&gt; $value1) { foreach ($plusMinus as $key2 =&gt; $value2) { if ($value1['0'] !== $value2['date']) { $value1['plusMinus'] = '0'; $result2[$key1][] = $value1; } elseif ($value1['0'] == $value2['date']) { if (array_key_exists('plus',$value2)) { $value1['plus'] = $value2['plus']; $result2[$key1][]=$value1; } elseif(array_key_exists('minus',$value2)) { $value1['minus'] = $value2['minus']; $result2[$key1][]=$value1; } elseif(array_key_exists('minus',$value2) &amp;&amp; array_key_exists('plus',$value2)) { } } } } $valuesComplete = array(); foreach ($result2 as $value) { $result2 = $value['0']; array_push($valuesIncomplete, $result2); } return $valuesComplete; </code></pre> <p>Instead of the desired outcome described above I get this:</p> <pre><code>Array ( [0] =&gt; Array ( [0] =&gt; 2007 [plus] =&gt; 2 ) [1] =&gt; Array ( [0] =&gt; 2008 [plusMinus =&gt; 0 ) [2] =&gt; Array ( [0] =&gt; 2009 [plusMinus] =&gt; 0 ) [3] =&gt; Array ( [0] =&gt; 2010 [plusMinus] =&gt; 0 ) [4] =&gt; Array ( [0] =&gt; 2011 [plusMinus] =&gt; 0 ) [5] =&gt; Array ( [0] =&gt; 2012 [plusMinus] =&gt; 0 ) [6] =&gt; Array ( [0] =&gt; 2013 [plusMinus] =&gt; 0 ) ) </code></pre> <p>What am I missing? Thanks for any help!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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