Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>&lt;?php function array_between_duplicates($ary) { // first, tally up all the values // we need to know how many times each value repeats $count = array_count_values($ary); // next, we want only the values that are not repeated. // This can be done by filtering the array for values // present 2+ times $between = array_filter($count, create_function('$a','return $a==1;')); // now that we have the unique values, swap the keys // and value locations using array_keys $swap = array_keys($between); // and then intersect the new array with the original // array so we can get back their original key values. $intersect = array_intersect($ary, $swap); var_dump($intersect); // now, in order to get the nested groups we will use // skipped keys as a sign that the in-between values // were repeats. So, iterate over the array and break // out these groups $result = array(); $group = array(); foreach ($ary as $key =&gt; $value) { if (!array_key_exists($key, $intersect) &amp;&amp; count($group) &gt; 0) { $result[] = $group; $group = array(); } if (array_search($value,$intersect) !== false) $group[] = $value; } if (count($group) &gt; 0) $result[] = $group; // return the result return $result; } var_dump(array_between_duplicates($Produtos)); </code></pre> <p>Results in:</p> <pre><code>array(3) { [0]=&gt; array(2) { [0]=&gt; string(5) "Piano" [1]=&gt; string(5) "Baixo" } [1]=&gt; array(2) { [0]=&gt; string(7) "Violão" [1]=&gt; string(13) "Caixas de Som" } [2]=&gt; array(1) { [0]=&gt; string(7) "Sanfona" } } </code></pre> <p><strong><a href="http://www.ideone.com/MQEZH" rel="nofollow">DEMO</a></strong></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