获取重复值之间的索引

时间:2011-08-25 00:45:55

标签: php arrays

如何从重复索引和数组中的其他索引中获取所有内容?见:

$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
    ) )       

4 个答案:

答案 0 :(得分:1)

可以这样解决:

<?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"
  }
}

在此期间,我已经理解了你真正想要的结果。我现在改变了它,它也可以进行多次重复,并始终从零开始。

答案 1 :(得分:1)

Se a a asoluçãocarceiro,podes conferirocódigomhttp://codepad.org/dKrKNlGH
Espero que te tenha ajudado,saudaçõessusitanas:)

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


$answer = array_count_values($Produtos);
$numero1 = 0;
$numero2 = 0;
$novos_produtos = array();
foreach($Produtos as $itens)
{
if( $answer[$itens] == 1)
{
$novos_produtos[$numero1][$numero2] =  $itens;  
$numero2++;
}else{
if(isset($novos_produtos[$numero1]))
$numero1++; 
$numero2 = 0;   
}
}
print_r($novos_produtos);
?>

<强> Resultado:

Array
(
    [0] => Array
        (
            [0] => Piano
            [1] => Baixo
        )

    [1] => Array
        (
            [0] => Violão
            [1] => Caixas de Som
        )

    [2] => Array
        (
            [0] => Sanfona
        )

)

答案 2 :(得分:0)

<?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

答案 3 :(得分:0)

$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);
相关问题