Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I know this was already answered, but I used this and extended it a little more in my code so that you didn't have search by only the uid. I just want to share it for anyone else who may need that functionality.</p> <p>Here's my example and please bare in mind this is my first answer. I took out the param array because I only needed to search one specific array, but you could easily add it in. I wanted to essentially search by more than just the uid.</p> <p>Also, in my situation there may be multiple keys to return as a result of searching by other fields that may not be unique.</p> <pre><code> /** * @param array multidimensional * @param string value to search for, ie a specific field name like name_first * @param string associative key to find it in, ie field_name * * @return array keys. */ function search_revisions($dataArray, $search_value, $key_to_search) { // This function will search the revisions for a certain value // related to the associative key you are looking for. $keys = array(); foreach ($dataArray as $key =&gt; $cur_value) { if ($cur_value[$key_to_search] == $search_value) { $keys[] = $key; } } return $keys; } </code></pre> <p>Later, I ended up writing this to allow me to search for another value and associative key. So my first example allows you to search for a value in any specific associative key, and return all the matches. </p> <p>This second example shows you where a value ('Taylor') is found in a certain associative key (first_name) <strong>AND</strong> another value (true) is found in another associative key (employed), and returns all matches (Keys where people with first name 'Taylor' AND are employed).</p> <pre><code>/** * @param array multidimensional * @param string $search_value The value to search for, ie a specific 'Taylor' * @param string $key_to_search The associative key to find it in, ie first_name * @param string $other_matching_key The associative key to find in the matches for employed * @param string $other_matching_value The value to find in that matching associative key, ie true * * @return array keys, ie all the people with the first name 'Taylor' that are employed. */ function search_revisions($dataArray, $search_value, $key_to_search, $other_matching_value = null, $other_matching_key = null) { // This function will search the revisions for a certain value // related to the associative key you are looking for. $keys = array(); foreach ($dataArray as $key =&gt; $cur_value) { if ($cur_value[$key_to_search] == $search_value) { if (isset($other_matching_key) &amp;&amp; isset($other_matching_value)) { if ($cur_value[$other_matching_key] == $other_matching_value) { $keys[] = $key; } } else { // I must keep in mind that some searches may have multiple // matches and others would not, so leave it open with no continues. $keys[] = $key; } } } return $keys; } </code></pre> <p><strong>Use of function</strong> </p> <pre><code>$data = array( array( 'cust_group' =&gt; 6, 'price' =&gt; 13.21, 'price_qty' =&gt; 5 ), array( 'cust_group' =&gt; 8, 'price' =&gt; 15.25, 'price_qty' =&gt; 4 ), array( 'cust_group' =&gt; 8, 'price' =&gt; 12.75, 'price_qty' =&gt; 10 ) ); $findKey = search_revisions($data,'8', 'cust_group', '10', 'price_qty'); print_r($findKey); </code></pre> <p><strong>Result</strong> </p> <pre><code>Array ( [0] =&gt; 2 ) </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