php中一维或多维数组去除重复项
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php中一维或多维数组去除重复项相关的知识,希望对你有一定的参考价值。
方案一$arr = array("0a","1b","2c","3d","4e","5f","6g","0a","2c","4e","6g","6g");
$arr1 = array_flip($arr);
$arr2 = array_flip($arr1);
print_r($arr2);
方案二
$arr = array("0a","1b","2c","3d","4e","5f","6g","0a","2c","4e","6g","6g");
$a = array_count_values($arr);
$b = array_keys($a);
print_r($b);
方案三
$arr = array("0a","1b","2c","3d","4e","5f","6g","0a","2c","4e","6g","6g");
foreach($arr as $k=>$v)
$arr1[$v] = $k;
$arr2 = array_flip($arr1);
print_r($arr2);
方案四
$arr = array("0a","1b","2c","3d","4e","5f","6g","0a","2c","4e","6g","6g");
$arr1 = array_unique($arr);
print_r($arr1);
都是刚刚我实验出来的 都可以封装起来用递归实现多维去掉重复项来自:求助得到的回答 参考技术A array_unique($array);
如果两个值匹配,则从 php 中的多维关联数组中删除重复项
【中文标题】如果两个值匹配,则从 php 中的多维关联数组中删除重复项【英文标题】:Remove duplicates from multidimensional associative array in php if two values match 【发布时间】:2014-04-14 16:26:53 【问题描述】:我有一个以下结构的多维数组,我想从中删除重复项。例如,如果两个 ["cities"] 的 ["amount"] 相同,但 ["time"] 相同或不同,那么我认为这是重复的,并希望从数组中删除此节点。
在下面的示例中,我想从数组中完全删除节点 0,因为城市和数量与节点 1 相同。它们都是布里斯托尔(英国布里斯托尔)和 373,尽管时间不同的是 17: 15 点和 17 点 16 分。
如果时间与这种情况不同,那么我会删除以后的时间。
array(8)
[0]=>
array(3)
["time"]=>
string(5) "17:16"
["city"]=>
string(33) "Bristol (Bristol, United Kingdom)"
["amount"]=>
int(373)
[1]=>
array(3)
["time"]=>
string(5) "17:15"
["city"]=>
string(33) "Bristol (Bristol, United Kingdom)"
["amount"]=>
int(373)
[2]=>
array(3)
["time"]=>
string(5) "17:16"
["city"]=>
string(37) "Wednesbury (Sandwell, United Kingdom)"
["amount"]=>
int(699)
[3]=>
array(3)
["time"]=>
string(5) "17:16"
["city"]=>
string(45) "Wolverhampton (Wolverhampton, United Kingdom)"
["amount"]=>
int(412)
[4]=>
array(3)
["time"]=>
string(5) "17:15"
["city"]=>
string(33) "Swansea (Swansea, United Kingdom)"
["amount"]=>
int(249)
[5]=>
array(3)
["time"]=>
string(5) "17:16"
["city"]=>
string(39) "Watford (Hertfordshire, United Kingdom)"
["amount"]=>
int(229)
[6]=>
array(3)
["time"]=>
string(5) "17:14"
["city"]=>
string(39) "Nottingham (Nottingham, United Kingdom)"
["amount"]=>
int(139)
[7]=>
array(3)
["time"]=>
string(5) "17:13"
["city"]=>
string(31) "Dartford (Kent, United Kingdom)"
["amount"]=>
int(103)
【问题讨论】:
【参考方案1】:试试这个:
$result = array();
foreach ($array as $place)
if (!array_key_exists($place['time'], $result))
$result[$place['time']] = $place;
【讨论】:
【参考方案2】:<?php
$data = array(
array(
'time' => '17:16',
'city' => 'Bristol',
'amount' => 373,
),
array(
'time' => '18:16',
'city' => 'Bristol',
'amount' => 373,
),
array(
'time' => '18:16',
'city' => 'Wednesbury',
'amount' => 699,
),
array(
'time' => '19:16',
'city' => 'Wednesbury',
'amount' => 699,
),
);
$tmp = array();
foreach ($data as $row)
$city = $row['city'];
$amount = $row['amount'];
if (!isset($tmp[$city][$amount])
|| $tmp[$city][$amount]['time'] < $row['time'])
$tmp[$city][$amount] = $row;
$data = array();
foreach ($tmp as $cities)
foreach ($cities as $city)
$data[] = $city;
var_dump($data);
【讨论】:
感谢这个工作,我只需要将 $tmp[$city][$amount]['time'] $row['time'] 以便保持较早的时间和较晚的时间。【参考方案3】:创建一个二维关联数组,其中一维键控城市,另一维是金额:
$assoc = array();
foreach ($data as $el)
$city = $el['city'];
$amount = $el['amount'];
if (isset($assoc[$city])
$assoc[$city][$amount] = $el;
else
$assoc[$city] = array($amount => $el);
// Now gather up all the elements back into a single array
$result = array();
foreach ($assoc as $cities)
foreach ($cities as $city)
$result[] = $city;
【讨论】:
以上是关于php中一维或多维数组去除重复项的主要内容,如果未能解决你的问题,请参考以下文章