douchen2595 2017-11-06 09:40
浏览 83
已采纳

如何在PHP中删除重复的数组对象?

I have this code below which will get the array object.

// fetch all circuits
$circuits = $Tools->fetch_all_circuits();

// check locations
if($circuits!==false) {
    // all locations
    $all_locations = array ();
    // loop
    foreach ($circuits as $circuit) {
        // format points
        $locationA = $Tools->reformat_circuit_location ($circuit->device1, $circuit->location1);
        $locationB = $Tools->reformat_circuit_location ($circuit->device2, $circuit->location2);

        if($locationA['location']!="" && $locationB['location']!="") {
            $locA = $Tools->fetch_object ("locations", "id", $locationA['location']);
            $locB = $Tools->fetch_object ("locations", "id", $locationB['location']);
            // save to all_locations array
            if ($locA!==false && $locB!==false) {
                $all_locations[] = $locA;
                $all_locations[] = $locB;
            }
        }
    }
}

var_dump ($all_locations);

How can i get the unique object from this array?

i var_dump($all_locations), i have this array output (i deleted some object, to lessen the output).

array(28) { [0]=> object(stdClass)#51 (6) { ["id"]=> string(3) "950"
["name"]=> string(4) "5829" ["description"]=> string(4) "Null"
["address"]=> string(4) "Null" ["lat"]=> string(7) "21.5336"
["long"]=> string(7) "39.2178" } [1]=> object(stdClass)#52 (6) {
["id"]=> string(3) "987" ["name"]=> string(4) "5765" ["description"]=>
string(4) "Null" ["address"]=> string(4) "Null" ["lat"]=> string(7)
"21.5439" ["long"]=> string(7) "39.2243" } [2]=> object(stdClass)#54
(6) { ["id"]=> string(4) "1016" ["name"]=> string(4) "2228"
["description"]=> string(4) "Null" ["address"]=> string(4) "Null"
["lat"]=> string(7) "21.5447" ["long"]=> string(7) "39.2301" } [27]=>
object(stdClass)#84 (6) { ["id"]=> string(4) "2536" ["name"]=>
string(4) "2389" ["description"]=> string(4) "Null" ["address"]=>
string(4) "Null" ["lat"]=> string(6) "21.495" ["long"]=> string(6)
"39.966" } }
  • 写回答

1条回答 默认 最新

  • dongyingming8970 2017-11-06 10:16
    关注

    array_unique() will help you in that. http://php.net/manual/en/function.array-unique.php

    array_unique() sorts the values treated as a string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题