Note that there are some explanatory texts on larger screens.

plurals
  1. POGroup pairs in array
    text
    copied!<p>I'm trying to group airlines with relations into single chains.</p> <pre><code>Array ( [0] =&gt; Array ( [0] =&gt; Aeroflot [1] =&gt; S7 [2] =&gt; Transaero ) [1] =&gt; Array ( [0] =&gt; Alitalia [1] =&gt; Lufthansa ) [2] =&gt; Array ( [0] =&gt; Transaero [1] =&gt; United ) [3] =&gt; Array ( [0] =&gt; United [1] =&gt; Alitalia ) [4] =&gt; Array ( [0] =&gt; Volotea [1] =&gt; Iberia ) [5] =&gt; Array ( [0] =&gt; Transaero [1] =&gt; Aeroflot ) ) </code></pre> <p>From that array I need to find connections between elements and combine it to groups. Expected results:</p> <pre><code>Array ( [0] =&gt; Array ( [0] =&gt; Aeroflot [1] =&gt; S7 [2] =&gt; Transaero [3] =&gt; United [4] =&gt; Alitalia [5] =&gt; Lufthansa ) [1] =&gt; Array ( [0] =&gt; Volotea [1] =&gt; Iberia ) ) </code></pre> <p>Can anyone help with that? I've tried a dozen of ways but still get no success.</p> <p>The most closest way I've tried which works but not in all cases:</p> <pre><code> function array_searchRecursive($needle,$haystack) { foreach($haystack as $key=&gt;$value) { $current_key=$key; if($needle===$value OR (is_array($value) &amp;&amp; array_searchRecursive($needle,$value) !== false)) { return $current_key; } } return false; } foreach ($newarr as $key =&gt; $airlines) { foreach ($airlines as $lastkey =&gt; $airline) { $index = array_searchRecursive($airline,$newarr); echo $airline.$index."\n"; if ($index !== false) { $newarr[$index] = array_merge($newarr[$index],$airlines); $lastarr[] = $index; } } } </code></pre> <p>But it doesn't match all values in array.</p>
 

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