谁能告诉我如何从两个数组中删除相同的值?我尝试过但无法得到正确的结果。

时间:2015-08-20 07:02:58

标签: php codeigniter

我尝试过array_merge和array_unique,但它无法帮助我。    这是我的代码。

    public function syncContacts() {
        $data['data'] = $this->main_manager->select_all('users'); # get all user data
        $info[0] = [
            "contact_name" => "a",
            "number" => "031651651"
        ];
        $info[1] = [
            "contact_name" => "b",
            "number" => "+923402382972"
        ];
        $info[2] = [
            "contact_name" => "c",
            "number" => "31651651"
        ];
        $info[3] = [
            "contact_name" => "d",
            "number" => "6165165123323"
        ];
        $info[4] = [
            "contact_name" => "e",
            "number" => "316516512113"
        ];

        for ($i = 0; $i < count($info); $i++) { # info array loop
            if (substr($info[$i]['number'], 0, 1) == "+") { # finds + sign in numbers
                for ($x = 0; $x < count($data['data']); $x++) { # user data array loop
                    if (
                            $info[$i]['number'] == $data['data'][$x]['country_code'] . $data['data'][$x]['phone_number']        # first condition
                            || $info[$i]['number'] == $data['data'][$x]['country_code'] . 0 . $data['data'][$x]['phone_number'] # second condition
                    ) {
                        $finalData['data']['registered'][$i]['name'] = $info[$i]['contact_name'];
                        $finalData['data']['registered'][$i]['number'] = $info[$i]['number'];
                    } else {
                        $finalData['data']['unregistered'][$i]['name'] = $info[$i]['contact_name'];
                        $finalData['data']['unregistered'][$i]['number'] = $info[$i]['number'];
                        $result = array_unique(array_merge($finalData['data']['unregistered'][$i], $info[$i]));
//        
                    }
 } # end of user data array loop
            } else { # finds 0 in numbers
                for ($x = 0; $x < count($data['data']); $x++) { # user data array loop
                    if (
                            $info[$i]['number'] == $data['data'][$x]['phone_number']        # first condition
                            || 0 . $info[$i]['number'] == $data['data'][$x]['phone_number'] # second condition
                            || $info[$i]['number'] == 0 . $data['data'][$x]['phone_number'] # third condition
                    ) {

                        $finalData['data']['registered'][$i]['name'] = $info[$i]['contact_name'];
                        $finalData['data']['registered'][$i]['number'] = $info[$i]['number'];
                    } else {
                        $finalData['data']['unregistered'][$i]['name'] = $info[$i]['contact_name'];
                        $finalData['data']['unregistered'][$i]['number'] = $info[$i]['number'];

                        $result = array_unique(array_merge($finalData['data']['unregistered'][$i], $info[$i]));
//                        $result = array_merge($finalData['data']['unregistered'][$i], $info[$i]);
//                        $finalData['data']['unregistered'][$i] = array_unique($result);
                    }
                } # end of user data array loop
            }
        } #end of info array loop 
        print_r($finalData);
        die();
    }

我正在制作同步联系人webservice。

在$ data数组中,我从users表中获取数据,其中有8条记录。 从我给定的数组匹配的3条记录是$ info,两条是无法比拟的。我创建了一个$ finalData [&#39; data&#39;]的数组。我必须将$ finalData数组的已注册和未注册键中的记录分开。我已经在数组的注册键中分隔了匹配的数字但是不能t在数组的未注册密钥中分隔正确的不匹配数字。 我得到了这个结果。

Array
(
    [data] => Array
        (
            [unregistered] => Array
                (
                    [0] => Array
                        (
                            [name] => a
                            [number] => 031651651
                        )

                    [1] => Array
                        (
                            [name] => b
                            [number] => +923402382972
                        )

                    [2] => Array
                        (
                            [name] => c
                            [number] => 31651651
                        )

                    [3] => Array
                        (
                            [name] => d
                            [number] => 6165165123323
                        )

                    [4] => Array
                        (
                            [name] => e
                            [number] => 316516512113
                        )

                )

            [registered] => Array
                (
                    [0] => Array
                        (
                            [name] => a
                            [number] => 031651651
                        )

                    [1] => Array
                        (
                            [name] => b
                            [number] => +923402382972
                        )

                    [2] => Array
                        (
                            [name] => c
                            [number] => 31651651
                        )

                )

        )

)

2 个答案:

答案 0 :(得分:1)

如何使用类似哈希的数组?请参阅此处示例http://php.net/manual/en/language.types.array.php

答案 1 :(得分:1)

请将第一个数组的索引与另一个匹配,并取消设置匹配的索引,然后您将获得未注册的数字。 我希望它会对你有所帮助