我有一个PHP数组的以下输出:
Array (
[0] => Array (
[set1] => Array (
[link] => xyz.com/def
[time] => 2016-01-03
[set2] => Array (
[link] =>
[time] => )
[set3] => Array (
[link] =>
[time] => )
)
[1] => Array (
[set1] => Array (
[link] => xyz.com/123
[time] => 2016-01-03)
[set2] => Array (
[link] =>
[time] => )
[set3] => Array (
[link] => xyz.com/123
[time] => 2016-01-03)
)
[2] => Array (
[set1] => Array (
[link] => xyz.com/abc
[time] => 2015-12-03)
[set2] => Array (
[link] => xyz.com/abc
[time] => 2016-01-03)
[set3] => Array (
[link] => xyz.com/123456
[time] => 2016-01-03)
) )
我想消除重复的[link]
,无论[time]
- 然后我想消除空[link]
值,例如[0][set2]
,最后有一个输出如下:
[link] => xyz.com/def
[time] => 2016-01-03
[link] => xyz.com/123
[time] => 2016-01-03)
[link] => xyz.com/abc
[time] => 2015-12-03
[link] => xyz.com/123456
[time] => 2016-01-03
这就是我的尝试:
$categoryUrlArray= array_unique(($categoryUrlArray, SORT_REGULAR);
foreach ($categoryUrlArray as $categoryUrlLevel01) {
$categoryUrlLevel01= array_unique($categoryUrlLevel01, SORT_REGULAR);
foreach ($categoryUrlLevel01 as $categoryUrlLevel02) {
echo $categoryUrlLevel02['link'];
echo '<br/>';
echo $categoryUrlLevel02['time'];
echo '<br/><br/><br/>';
}
}
上面代码的问题是没有删除重复项并且仍然显示空值,即有大量换行<br/>
- 这就是为什么我知道它们没有被淘汰。
答案 0 :(得分:2)
如果只需检查inner loop
和blanks
的{{1}}内部怎么样?然后将它们全部保存在一个新变量中。像这样
duplicates
将导致
$newArr = array();
$links = array();
foreach($categoryUrlArray as $urls){
foreach($urls as $url){
if(!empty($url['link']) && !in_array($url['link'],$links)){
$newArr[] = $url;
$links[] = $url['link'];
}
}
}
var_dump($newArr);
答案 1 :(得分:0)
只需添加条件即可检查链接是否为空!empty($categoryUrlLevel02['link'])
以下是修改后的代码:
($categoryUrlArray= array_unique(($categoryUrlArray, SORT_REGULAR);
foreach ($categoryUrlArray as $categoryUrlLevel01) {
$categoryUrlLevel01= array_unique($categoryUrlLevel01, SORT_REGULAR);
foreach ($categoryUrlLevel01 as $categoryUrlLevel02) {
if(!empty($categoryUrlLevel02['link']))
{
echo $categoryUrlLevel02['link'];
echo '<br/>';
echo $categoryUrlLevel02['time'];
echo '<br/><br/><br/>';
}
}
}
答案 2 :(得分:0)
通过在结果数组中分配临时键并检查!isset()
中的识别键(即link
值),可以有效地识别结果数组中的唯一值的最佳实践。如果您不想在输出中看到这些冗余/临时键,请致电array_values()
。
* note,相对于in_array()
,在性能上进行isset()
的迭代调用肯定会更加昂贵。
代码:(Demo)
$categoryUrlArray = [
[
"set1" => ["link" => "xyz.com/def", "time" => "2016-01-03"],
"set2" => ["link" => "", "time" => ""],
"set3" => ["link" => "", "time" => ""]
],
[
"set1" => ["link" => "xyz.com/123", "time" => "2016-01-03"],
"set2" => ["link" => "", "time" => ""],
"set3" => ["link" => "xyz.com/123", "time" => "2016-01-03"]
],
[
"set1" => ["link" => "xyz.com/abc", "time" => "2016-12-03"],
"set2" => ["link" => "xyz.com/abc", "time" => "2016-01-03"],
"set3" => ["link" => "xyz.com/123456", "time" => "2016-01-03"]
]
];
foreach ($categoryUrlArray as $groups) {
foreach ($groups as $row) {
if ($row["link"] && !isset($result[$row["link"]])) { if "truthy" (not empty) and unique
$result[$row["link"]] = $row;
}
}
}
var_export(array_values($result));
输出:
array (
0 =>
array (
'link' => 'xyz.com/def',
'time' => '2016-01-03',
),
1 =>
array (
'link' => 'xyz.com/123',
'time' => '2016-01-03',
),
2 =>
array (
'link' => 'xyz.com/abc',
'time' => '2016-12-03',
),
3 =>
array (
'link' => 'xyz.com/123456',
'time' => '2016-01-03',
),
)