猿问

从PHP中的多维数组中删除重复数组

如何从这个数组中过滤重复的值?


实际上,对于country和city,数据是相同的-除了总体发生变化之外。


如何删除包含更高人口的阵列?


$arr = array

(

    "100" => array(

        array(

            "country" => 'France',

            "city" => 'Paris',

            "population" => '1800000',

        ),

        array(

            "country" => 'France',

            "city" => 'Paris',

            "population" => '2000000',

        ),

        array(

            "country" => 'France',

            "city" => 'Toulouse',

            "population" => '500000',

        ),

    )

    "101" => array(

        array(

            "country" => 'Russia',

            "city" => 'Moscow',

            "population" => '144000000',

        )

    )

);

因此,所需的输出应为:


$arr = array

(

    "100" => array(

        array(

            "country" => 'France',

            "city" => 'Paris',

            "population" => '1800000'

        ),

        array(

            "country" => 'France',

            "city" => 'Toulouse',

            "population" => '500000'

        ),

    )

    "101" => array(

        array(

            "country" => 'Russia',

            "city" => 'Moscow',

            "population" => '144000000',

        )

    )

);

这是我尝试的:


$temp_array = [];

foreach ($array as &$v) {

    if (!isset($temp_array[$v['country']] && $temp_array[$v['city']]))

        $temp_array[$v[$key]] =& $v;

    }

$array = array_values($temp_array);

return $array;


交互式爱情
浏览 136回答 3
3回答

ITMISS

您可以首先使用array_reduce过滤较低的人口(使用国家和城市的组合作为关键字)。然后将它们炸开并使用该最小值重置数组:foreach($arr as $k => &$ar) {    $temp = array_reduce($ar, function ($carry, $item) {        $key = $item["country"] . "###" . $item["city"];        $carry[$key] = (isset($carry[$key]) && $item["population"] > $carry[$key]) ?  $carry[$key] : $item["population"];        return $carry;    }, []);    $ar = [];    foreach($temp as $k => $val) {        list($country, $city) = explode("###", $k);        $ar[] = array("country" => $country, "city" => $city, "population" => $val);    }}直播示例:3lv4编辑:您可以使用array_filter,而不是foreach循环,以避免应对:$ar = array_filter($ar, function ($item) use ($mins) {    $key = $item["country"] . "###" . $item["city"];    return $mins[$key] == $item["population"]; });

智慧大石

我做了两个嵌套循环,第一个嵌套子数组包含一个特定键(例如100和101)的所有内容。接下来,我遍历数据,并保留一个包含两个级别的临时数组,第一个级别将国家作为关键,第二个级别将城市作为跟踪最低人口的关键。完成上述操作后,我将遍历临时数组以正确的格式获取国家,城市和人口,并将其附加到新数组中。然后,我用以前的数组替换这个新获取的结果。<?php$arr = array(&nbsp; &nbsp; "100" => array(&nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "country" => 'France',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "city" => 'Paris',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "population" => '1800000',&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "country" => 'France',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "city" => 'Paris',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "population" => '2000000',&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "country" => 'France',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "city" => 'Toulouse',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "population" => '500000',&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; ),&nbsp; &nbsp; "101" => array(&nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "country" => 'Russia',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "city" => 'Moscow',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "population" => '144000000',&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; ));foreach($arr as $key=>$subarr) {&nbsp; &nbsp; $tmp = array();&nbsp; &nbsp; foreach($subarr as $v) {&nbsp; &nbsp; &nbsp; &nbsp; $country = $v['country'];&nbsp; &nbsp; &nbsp; &nbsp; $city = $v['city'];&nbsp; &nbsp; &nbsp; &nbsp; $population = $v['population'];&nbsp; &nbsp; &nbsp; &nbsp; if(isset($tmp[$country])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isset($tmp[$country][$city])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($tmp[$country][$city] > $population) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tmp[$country][$city] = $population;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tmp[$country][$city] = $population;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tmp[$country] = array();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tmp[$country][$city] = $population;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; $res = array();&nbsp; &nbsp; &nbsp; &nbsp; foreach($tmp as $country=>$cities) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($cities as $city=>$population) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $res[] = array('country'=>$country,'city'=>$city,'population'=>$population);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; $arr[$key] = $res;}print_r($arr);
随时随地看视频慕课网APP
我要回答