我有以下数组
array:3 [
0 => array:3 [
0 => "EN"
1 => "ENGLISH"
2 => 1
]
1 => array:3 [
0 => "JA"
1 => "JAPANESE"
2 => 1
]
2 => array:3 [
0 => "JA"
1 => "JAPANESE"
2 => 0
]
]我想删除副本,但只检查键、和1。当我使用array_unique()时,它不起作用。
我期望的结果是
array:2 [
0 => array:3 [
0 => "EN"
1 => "ENGLISH"
2 => 1
]
1 => array:3 [
0 => "JA"
1 => "JAPANESE"
2 => 1
]
]最后一个数组被移除,因为它具有相同的JA和JAPANESE,还有一个具有1的数组。
提前谢谢。
发布于 2017-07-20 02:39:52
使用唯一索引作为键,
$result = [];
foreach($array as $v)
{
$result[$v[0] . $v[1]] = $v;
}
$result = array_values($result);发布于 2017-07-20 02:33:33
使用集合,您可以通过
// using collection
$collection = collect([ ["EN", "ENGLISH", 1],["JP", "JAPAN", 1], ["JP", "JAPAN", 1] ]);
// then filtering the value
$filtered = $collection->filter(function ($value, $key) {
return $value[2] == 1;
});
// then unique only by the acronym ( en , jp )
$unique = $filtered->unique(0);
// you may also add the 2nd value to determine it's uniqueness
$unique = $filtered->unique(function ($item) {
return $item[0].$item[1];
});
// getting all the uniqued values
$unique->values()->all();https://stackoverflow.com/questions/45204008
复制相似问题