Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Quoted from <a href="https://stackoverflow.com/questions/6791479/search-array-array-filter-vs-loop#comment-8060388">Search Array : array_filter vs loop</a>:</p> <blockquote> <p><code>array_filter()</code> cannot handle [multi-dimensional arrays] natively. You're looking for a single value inside an array? <code>array_filter()</code> is not the best way to do this because you can stop iteration when you found the value you've been looking for - <code>array_filter()</code> doesn't do that. Filter a set of values from a larger set? Most likely that <code>array_filter()</code> is faster than a hand-coded <code>foreach</code>-loop because it's a built-in function. – <a href="https://stackoverflow.com/users/11354/stefan-gehrig">Stefan Gehrig</a></p> </blockquote> <p>Using a <a href="http://php.net/manual/en/control-structures.foreach.php" rel="nofollow noreferrer">php <code>foreach</code> loop</a> is probably the easier of the two to read:</p> <pre><code>function getItem($haystack, $needle) { foreach ($haystack as $hay) { if ($hay-&gt;meta_key == $needle) { return $hay-&gt;meta_value; } } return FALSE; } echo getItem($items, 'Email of Attendee'); // Returns 'some-email@gmail.com' </code></pre> <p>However, as the quote supposes, for a larger array, you may want to go with something like php's <a href="http://php.net/manual/en/function.array-filter.php" rel="nofollow noreferrer"><code>array_filter()</code></a>:</p> <pre><code>function metaKeyIsEmail($obj) { return $obj-&gt;meta_key == 'Email of Attendee'; } // array_filter() will return an array containing all items // that returned TRUE for the callback metaKeyIsEmail() $items_matched = array_filter($items, 'metaKeyIsEmail'); // If there was at least one match, take it off the front of // the array and get its meta_value. Otherwise use FALSE. $matched_value = !empty($items_matched) ? array_shift($items_matched)-&gt;meta_value : FALSE; echo $matched_value; // Returns 'some-email@gmail.com' </code></pre>
    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. 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