首先,我意识到这可能看似重复,因为我已经阅读了一些关于类似主题的问题(1,2),但我很难看到如何重新设计适合我的Senario的代码库。
我正在尝试获取现有的多维数组并删除在特定字段中具有重复的任何节点。这是我正在使用的数据集:
array(3) {
[0]=>
array(3) {
["company"]=>
string(9) "Company A"
["region"]=>
string(4) "EMEA"
["ctype"]=>
string(8) "Customer"
}
[1]=>
array(3) {
["company"]=>
string(9) "Company A"
["region"]=>
string(4) "EMEA"
["ctype"]=>
string(8) "Customer"
}
[2]=>
array(3) {
["company"]=>
string(9) "Company C"
["region"]=>
string(4) "EMEA"
["ctype"]=>
string(8) "Customer"
}
}
如果这不是多维数组,则使用in_array()查看dataset['company']
是否存在。如果没有,我会将它添加到我的$unique
数组中,如下所示:
$unique = array();
foreach ($dataset as $company) {
$company_name = $company['company'];
if ( !in_array($company_name, $unique) ) {
array_push($unique, $company_name);
}
}
var_dump($unique);
但是我不确定如何遍历多维数组以获取['company']
数据以查看它是否存在(因为它是我需要检查的唯一项目,看它是否已经存在)。
我希望输出与初始数据集完全相同的数据,只需删除重复数据。请你指点我正确的方向吗?
答案 0 :(得分:1)
将已经检查过的公司存放在一些副阵列中:
$unique = array();
$companies = array();
foreach ($dataset as $company) {
$company_name = $company['company'];
if ( !in_array($company_name, $companies) ) {
array_push($unique, $company);
array_push($companies, $company_name);
}
}
var_dump($unique);
答案 1 :(得分:1)
将array_filter
与use
关键字一起使用,并使用引用数组。
>>> $data
=> [
[
"company" => "Company A",
"region" => "EMEA",
"ctype" => "Customer"
],
[
"company" => "Company A",
"region" => "EMEA",
"ctype" => "Customer"
],
[
"company" => "Company C",
"region" => "EMEA",
"ctype" => "Customer"
]
]
$whitelist = [];
array_filter($data, function ($item) use (&$whitelist) {
if (!in_array($item['company'], $whitelist)) {
$whitelist[] = $item['company'];
return true;
};
return false;
});
=> [
0 => [
"company" => "Company A",
"region" => "EMEA",
"ctype" => "Customer"
],
2 => [
"company" => "Company C",
"region" => "EMEA",
"ctype" => "Customer"
]
]
答案 2 :(得分:0)
要重建没有重复项的数组:
$result = array();
foreach($datas as $data){
foreach($data as $key => $value){
$result[$key][$value] = $value;
}
}
print_r($result);
输出:
Array
(
[company] => Array
(
[Company A] => Company A
[Company C] => Company C
)
[region] => Array
(
[EMEA] => EMEA
)
[ctype] => Array
(
[Customer] => Customer
)
)
保持相同的架构:
$datas = array(
array(
"company"=>"Company A",
"region"=>"EMEA",
"ctype"=>"Customer"
),
array(
"company"=>"Company A",
"region"=>"EMEA",
"ctype"=>"Customer"
),
array(
"company"=>"Company C",
"region"=>"EMEA",
"ctype"=>"Customer"
)
);
function removeDuplicateOnField($datas, $field){
$result = array();
foreach($datas as $key => &$data){
if(isset($data[$field]) AND !isset($result[$data[$field]])){
$result[$data[$field]] = $data;
}
else
unset($datas[$key]);
}
return $datas;
}
$result = removeDuplicateOnField($datas, "company");
print_r($result);
答案 3 :(得分:0)
你似乎在描述的是PHP已经可以满足的东西。您之前听说过array_unique功能吗?它不能递归地工作,但在浏览PHP文档时,有人已经创建了一个可以工作的函数。
多行数组的唯一递归数组
function super_unique($array)
{
$result = array_map("unserialize", array_unique(array_map("serialize", $array)));
foreach ($result as $key => $value)
{
if ( is_array($value) )
{
$result[$key] = super_unique($value);
}
}
return $result;
}
让我知道这是否有效,因为我目前不在办公室。