Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I might be misunderstanding, but this just sounds like the accessor. If not, could you clarify?</p> <pre><code>$array= array( 'Book1' =&gt; array('http://www.google.com', '45' ), 'Book2' =&gt; array('http://www.yahoo.com', '46', ) ); echo $array['Book1']; </code></pre> <p>EDIT: I did misunderstand your goal. I do have a comment on doing the two foreach loops. While this does work, when you have a very large haystack array, performance suffers. I would recommend using <code>isset()</code> for testing if a needle exists in the haystack array.</p> <p>I modified the function to return an array of the found results to remove any performance hits from outputing to stdout. I ran the following performance test and while it might not do the same search over and over, it points out the inefficiency of doing two foreach loops when your array(s) are large:</p> <pre><code>function array_searcher($needles, $array) { $result = array(); foreach ($needles as $needle) { foreach ($array as $key =&gt; $value) { if ($key == $needle) { $result[$key] = $value; } } } return $result; } function array_searcher2($needles, $array) { $result = array(); foreach ($needles as $needle) { if (isset($array[$needle])) { $result[$needle] = $array[$needle]; } } return $result; } // generate large haystack array $array = array(); for($i = 1; $i &lt; 10000; $i++){ $array['Book'.$i] = array('http://www.google.com', $i+20); } $needles = array('Book1', 'Book2'); $start = microtime(true); for ($i = 0; $i &lt; 1000; $i++) { array_searcher($needles, $array); } echo (microtime(true) - $start)."\n"; // 14.2093660831 $start = microtime(true); for ($i = 0; $i &lt; 1000; $i++) { array_searcher2($needles, $array); } echo (microtime(true) - $start)."\n"; // 0.00858187675476 </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