在重复值之间获取索引


take indexes between repeated values

如何从重复索引和数组中的另一个索引中获取所有内容?参见:

$Produtos = Array(  'Guitarra' , // repeats
                       'Bateria' ,  // repeats
                       'Piano' ,  
                       'Baixo' , 
                       'Guitarra' ,  // repeats
                       'Violão' , 
                       'Caixas de Som' ,
                       'Bateria' , // repeats
                       'Sanfona' );

审查了重复的指数吗,就像我所做的那样,以了解它们之间的区别?我希望返回:`

Array
(
    [0] => Array( 
              [0] => Piano
              [1] => Baixo
    [1] => Array( 
              [0] => Violão
              [1] => Caixas de Som
    [2] => Array( 
              [0] => Sanfona
    ) )       

可以这样解决:

<?php
<?php
$Produtos = Array(  'Guitarra' , // repeats
                       'Bateria' ,  // repeats
                       'Piano' ,
                       'Baixo' ,
                       'Guitarra' ,  // repeats
                       'Violão' ,
                       'Caixas de Som' ,
                       'Bateria' , // repeats
                       'Sanfona' );

$countedProducts = array_count_values($Produtos);
$c = 0;
foreach ($Produtos as $product)
{
    if ($countedProducts[$product] > 1)
    {
        if (count($novosProdutos))
        {
            $c++;
        }
    }else{
        $novosProdutos[$c][] = $product;
    }
}
print '<pre>';
var_dump($novosProdutos);
print '</pre>';
?>

输出:

array(3) {
  [0]=>
  array(2) {
    [0]=>
    string(5) "Piano"
    [1]=>
    string(5) "Baixo"
  }
  [1]=>
  array(2) {
    [0]=>
    string(7) "Violão"
    [1]=>
    string(13) "Caixas de Som"
  }
  [2]=>
  array(1) {
    [0]=>
    string(7) "Sanfona"
  }
}

与此同时,我已经明白了你真正想要的是什么。我现在改变了它,它也可以多次重复,并且总是从零开始。

$finalProducts = array();
$currentKey = 0;
$wordCount = array_count_values($Produtos);
foreach($Produtos as $val) {
    if($wordCount[$val] > 1) {
        $currentKey++;
    }
    elseif(strlen($currentKey) > 0) {
        $finalProducts[$currentKey][] = $val;
    }
}
$finalProducts = array_values($finalProducts);
<?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 => $value)
  {
    if (!array_key_exists($key, $intersect) && count($group) > 0)
    {
      $result[] = $group;
      $group = array();
    }
    if (array_search($value,$intersect) !== false)
      $group[] = $value;
  }
  if (count($group) > 0)
    $result[] = $group;
  // return the result
  return $result;
}
var_dump(array_between_duplicates($Produtos));

结果:

array(3) {
  [0]=>
  array(2) {
    [0]=>
    string(5) "Piano"
    [1]=>
    string(5) "Baixo"
  }
  [1]=>
  array(2) {
    [0]=>
    string(7) "Violão"
    [1]=>
    string(13) "Caixas de Som"
  }
  [2]=>
  array(1) {
    [0]=>
    string(7) "Sanfona"
  }
}

DEMO