Note that there are some explanatory texts on larger screens.

plurals
  1. POPerformance: search for values and return the sub arrays' key
    primarykey
    data
    text
    <p>Given an array like: </p> <pre><code>$nouns = array( "man" =&gt; array("men"), "octopus" =&gt; array("octopi", "octopuses"), "ox" =&gt; array("oxen") ); </code></pre> <p>Consisting of approximately 3000 singular - plural pairs, how would one get the singular (the key) the most efficiently, by calling, for example, <code>array_search_loosely($nouns, "men")</code>?</p> <p>I then expect to receive an array with the value "man".</p> <p>I have tried four different approaches:</p> <p><strong>Original</strong> (which uses the function <code>array_is_assoc</code>, which is rather self-explanatory and irrelevant to the situation)</p> <pre><code>function array_search_loosely($array, $values, $compare_keys = false) { $values = is_array($values) ? $values : array($values); foreach($array as $item_key =&gt; $item) { if (is_array($item)) { $return_key = true; foreach($values as $value_key =&gt; $value) { if (!in_array($value, $item)) { $return_key = false; break; } elseif($compare_keys === true and array_is_assoc($values)) { if (!in_array($value_key, array_keys($item, $value))) { $return_key = false; break; } } } if ($return_key === true) { $item_keys[] = $item_key; } } elseif(!is_array($values)) { if ($item === $values) { $item_keys[] = $item_key; } } } return (isset($item_keys))? $item_keys : false; } </code></pre> <p><strong>Second approach</strong>:</p> <pre><code>function array_search_loosely($array, $values, $compare_keys = false) { $keys = array_keys(array_filter($array, function($item) use ($values, $compare_keys) { return (!is_array($item) and $item === $values) or (is_array($item) and each_in_array($item, array_create($values), $compare_keys)); })); return !empty($keys) ? $keys : false; } function each_in_array($array, $values, $compare_keys = false) { return $compare_keys === false ? count(array_uintersect($values, $array, function($item1, $item2) { return $item1 === $item2 ? 0 : ($item1 &gt; $item2 ? 1 : -1); })) == count($values) : count(array_uintersect_assoc($values, $array, function($item1, $item2) { return $item1 === $item2 ? 0 : ($item1 &gt; $item2 ? 1 : -1); })) == count($values); } </code></pre> <p>I chose to use <code>array_uintersect</code>, to also allow arrays to be <code>$items</code>, because if I was to use array_intersect, notices would be generated for each array <code>$item</code>. This choice allows <code>each_in_array()</code> to check for array <code>$values</code> as well.</p> <p>Also, the third optional parameter <code>$compare_keys</code> is not relevant for this situation, but is in other situations where I use the function.</p> <p>The third and fourth approaches were mixtures of the preceding. At this point my original approach would still be the fastest, but when I run my function on a few hundred or thousand words, the operation will still cost several tens of seconds. Any suggestions on how to improve the performance of getting a plurals singular in this situation?</p>
    singulars
    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.
    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