Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you really need to do it this way, you could do something like the following:</p> <pre><code>foreach($arrayB as $bRow) { if(FALSE !== ($key = array_search($bRow, $arrayA)) { $arrayC[] = $arrayA[$key+1]; } } </code></pre> <p>However, a string-indexed array would probably be better for Array A. Use the following for the 3 examples below:</p> <pre><code>$arrayA = array("2012-08-01" =&gt; 1, "2012-08-03" =&gt; 1, "2012-08-07" =&gt; 2, "2012-08-09" =&gt; 2, "2012-08-27" =&gt; 1); $arrayB = array("2012-09-04", "2012-09-06", "2012-09-08", "2012-09-10"); $arrayC = array(); </code></pre> <p>If Array A always contains more elements than Array B (or the same number):</p> <pre><code>foreach($arrayA as $key=&gt;$value) { if(in_array($key, $arrayB)) { $arrayC[] = $value; } } </code></pre> <p>If Array B always contains more elements than Array A (or the same number):</p> <pre><code>foreach($arrayB as $bRow) { if(isset($arrayA[$bRow])) { $arrayC[] = $arrayA[$bRow]; } } </code></pre> <p>If it is possible that sometimes Array A is longer and sometimes Array B is longer:</p> <pre><code>$matchedIndexes = array(); foreach($arrayA as $key=&gt;$value) { if(in_array($key, $arrayB)) { $arrayC[] = $value; $matchedIndexes[] = $key; } } foreach($arrayB as $bRow) { // Only add the entries that we did not already add when looping through $arrayA if(!in_array($key, $matchedIndexes) &amp;&amp; isset($arrayA[$bRow])) { $arrayC[] = $arrayA[$bRow]; } } </code></pre> <p>Populating arrayC with zeros foreach entry in arrayA that does not have a match in arrayB:</p> <pre><code>foreach($arrayA as $key=&gt;$value) { if(in_array($key, $arrayB)) { $arrayC[] = $value; } else { $arrayC[] = 0; } } </code></pre>
 

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