从 php 数组中删除重复项(不是 array_unique)
Remove duplicate from php array (not array_unique)
我有一个这样的数组:
Array(
[0] => Array(
[id] => 1
[name] => name1
[table] => warehouse)
[1] => Array(
[id] => 2
[name] => name2
[table] => warehouse)
[2] => Array(
[id] => 3
[name] => name1
[table] => kitchen)
[3] => Array(
[id] => 4
[name] => name5
[table] => kitchen)
)
我想得到这个结果。 array_unique 不起作用,因为只有一个值相同,我只想按名称排序。 id 和 tables 可以相同。
Array(
[0] => Array(
[id] => 1
[name] => name1
[table] => warehouse)
[1] => Array(
[id] => 2
[name] => name2
[table] => warehouse)
[3] => Array(
[id] => 4
[name] => name5
[table] => kitchen))
这个怎么样?
它将只有唯一的记录。
如有多个类似记录,以最后一个为准。
$new = [];
foreach($array as $a) $new[$a['name']] = $a;
print_r($new);
如果您只希望发生的事情占上风,您可能必须先 array_reverse()
数组。
使用 2 个 foreach 语句你可以这样做..
让我们命名...
$arr1 = array("shayan","ali","shayan","maziar","pezhman","ali");
$arr2 = array();
foreach($arr1 as $element1)
{
foreach($arr1 as $element2)
{
if ($element1 != $element2 && !in_array($element1,$arr2))
{
array_push($arr2,$element1);
}
}
}
print_r($arr2);
希望对您有所帮助<3
试试这个
$data = Array(
[0] => Array(
[id] => 1
[name] => name1
[table] => warehouse)
[1] => Array(
[id] => 2
[name] => name2
[table] => warehouse)
[2] => Array(
[id] => 3
[name] => name1
[table] => kitchen)
[3] => Array(
[id] => 4
[name] => name5
[table] => kitchen)
);
$record = array();
$name = array();
foreach($data as $key=>$value){
if(!in_array($value['name'], $name)){
$name[] = $value['name'];
$record[$key] = $value;
}
}
print_r($record);
如果您不使用 vanilla-PHP,您可以使用 ouzo-goodies 库:
$uniqueByName = Arrays::uniqueBy($array, 'name');
您可以使用 ArrayHelper
与 Yii
框架捆绑在一起。
$array = array_values(ArrayHelper::index($array, 'name'));
我有一个这样的数组:
Array(
[0] => Array(
[id] => 1
[name] => name1
[table] => warehouse)
[1] => Array(
[id] => 2
[name] => name2
[table] => warehouse)
[2] => Array(
[id] => 3
[name] => name1
[table] => kitchen)
[3] => Array(
[id] => 4
[name] => name5
[table] => kitchen)
)
我想得到这个结果。 array_unique 不起作用,因为只有一个值相同,我只想按名称排序。 id 和 tables 可以相同。
Array(
[0] => Array(
[id] => 1
[name] => name1
[table] => warehouse)
[1] => Array(
[id] => 2
[name] => name2
[table] => warehouse)
[3] => Array(
[id] => 4
[name] => name5
[table] => kitchen))
这个怎么样?
它将只有唯一的记录。
如有多个类似记录,以最后一个为准。
$new = [];
foreach($array as $a) $new[$a['name']] = $a;
print_r($new);
如果您只希望发生的事情占上风,您可能必须先 array_reverse()
数组。
使用 2 个 foreach 语句你可以这样做..
让我们命名...
$arr1 = array("shayan","ali","shayan","maziar","pezhman","ali");
$arr2 = array();
foreach($arr1 as $element1)
{
foreach($arr1 as $element2)
{
if ($element1 != $element2 && !in_array($element1,$arr2))
{
array_push($arr2,$element1);
}
}
}
print_r($arr2);
希望对您有所帮助<3
试试这个
$data = Array(
[0] => Array(
[id] => 1
[name] => name1
[table] => warehouse)
[1] => Array(
[id] => 2
[name] => name2
[table] => warehouse)
[2] => Array(
[id] => 3
[name] => name1
[table] => kitchen)
[3] => Array(
[id] => 4
[name] => name5
[table] => kitchen)
);
$record = array();
$name = array();
foreach($data as $key=>$value){
if(!in_array($value['name'], $name)){
$name[] = $value['name'];
$record[$key] = $value;
}
}
print_r($record);
如果您不使用 vanilla-PHP,您可以使用 ouzo-goodies 库:
$uniqueByName = Arrays::uniqueBy($array, 'name');
您可以使用 ArrayHelper
与 Yii
框架捆绑在一起。
$array = array_values(ArrayHelper::index($array, 'name'));