Php 在重复值之间获取索引
如何从重复索引和数组中的其他索引中获取所有内容?见:Php 在重复值之间获取索引,php,arrays,Php,Arrays,如何从重复索引和数组中的其他索引中获取所有内容?见: $Produtos = Array( 'Guitarra' , // repeats 'Bateria' , // repeats 'Piano' , 'Baixo' , 'Guitarra' , // repeats
$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>';
?>
同时,我也明白了,你真正想要的结果是什么。我现在更改了它,它也可以多次重复工作,并且总是从零开始。你能更清楚地了解你想要什么吗/?更清楚吗?我解释了我需要的一切,是的,我储存了它们。哦,你能假装吗?重复性视网膜。这是一种非常奇怪的阵列结构方式。你到底想要什么(即:你为什么希望回报是现在的样子?)见我的回报,我希望为重复的项目中的每个项目创建一个索引。它现在正在工作。我第一次不理解这个任务,不幸的是我不会说葡萄牙语。是的,它在工作,甚至会说葡萄牙语。我一开始也没听懂,用户消失了,甚至没说“obrigado”:)是的,真的很烦人。看我的回复,我希望为每个重复的项目创建一个索引。成功了,谢谢,你看到杰夫的答案了吗?更一般。
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"
}
}
<?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);