Matching Needle & Haystack values using alternate Ontology values I need to match values in Needle &amp; Haystack arrays, using alternate Ontology values when relevant. 

This syntax works, however I would like to know if there is a more efficient way to do this as I will be using it for several corresponding $AltNeedle arrays for each of 1000+ $needles and $haystack arrays.

In my use case, the Needle &amp; Haystack correspond with Product and Customer attributes respectively, and matches are made and ranked by assessing thousands of attributes for each, and attributes often have an array of values. One way in which Alt. Values are used are to incorporate closely related values into certain match results.

&lt;?php
$needles = ['John', 'Alex', 'Pam'];
$haystack = ['John','Alexander','Kim', 'Michael'];
$AltNeedle = ['Alexander', 'Alex', 'Alexi'];

foreach ($needles as $current_needle) {
    if (in_array($current_needle, $AltNeedle) ) { // Exists in $AltNeedle
        foreach ($AltNeedle as $current_AltNeedle) {
                if (in_array($current_AltNeedle, $haystack) ) { // Compares $current_AltNeedle to $haystack
                    echo $current_AltNeedle." True; ";
                } 
        } // Ends $AltNeedle foreach
    }
    elseif (in_array($current_needle, $haystack) ) {
        echo $current_needle.": True; ";
    }
    else {
        echo $current_needle.": False; ";
    }                  
} // End foreach
?&gt;


RESULT:


  John: True; Alexander True; Pam: False;
The whole thing needs restructuring if you are worried about performance.  First, you probably shouldn't be using an array for your haystack.  Considering that you will know with certainty what you are looking for in the haystack, it should not be an array which must be iterated in \$O(n)\$ to determine presence of an item.  An associative array with haystack values as keys or even a stdClass item with haystack values as properties would allow for \$O(1)\$ lookup to determine if a value is present.  That might look something like this (perhaps you have more meaningful full data that true to put at each key):

[
    'John' =&gt; true,
    'Alexander' =&gt; true,
    'Kim' =&gt; true,
    'Michael' =&gt; true,
    ...
]


The same can be said for the data structure around the alternative needles.  There is no reason to perform an \$O(n)$\ iteration on the alt needle array to see if an item has a lookup value.  Imagine an associative array like:

[
    'Alex' =&gt; [
        'Alexander',
        'Alexi'
    ],
    'Alexi' =&gt; [
        'Alex',
        'Alexander'
    ],
    'Alexander' =&gt; [
        'Alex',
        'Alexi,
    ],
    ...
]


This could give you \$O(1)\$ access to an array of alternate names.

So you code might be changes to look like:

$results = [];
foreach($needles as $needle) {
   $names = [$needle];
   if(array_key_exists($needle, $altNeedles)) {
       $names = array_merge($searches, $altNeedles[$needle]);
   }
   foreach($names as $name) {
       if(array_key_exists($name, $haystack)) {
           $results[$name] = true;
       }
   }
}


Given:
    m = needles array size
    n = number of alt needles per given name
    p = haystack size

This reduces overall worst case complexity from \$O(m*n^2*p)\$  to \$O(m * n)\$

In best cases (single needles, no alternate names) this solution degenerates to \$O(1)\$ complexity, whereas, yours still always requires at least \$O(p)\$.

Now, if you have the opportunity to really rethink you data structures, this can be improved even further.  For example, are what you really calling "alternate needles" really simply alternates on the haystack?  In other words, can the alternate names simply become a part of the haystack such that you can get \$O(1)\$ look up for each needle?