数组合并并删除重复的问题PHP

时间:2012-05-14 13:51:55

标签: php arrays

我有两个数组,从结果中填充。

1)users_facebook& 2)users_twitter

Array
(
    [users_facebook] => Array
    (
        [0] => Array
            (
                [id] => "180",
                [email] => "",
                [first_name] => "Djon",
                [last_name] => "Katview"
        ),
        [1] => Array
            (
                [id] => "182",
                [email] => "",
                [first_name] => "Awaaru",
                [last_name] => "Bhejafry"
        )
    ),

    [users_twitter] => Array
    (
        [0] => Array
            (
                [id] => "180",
                [email] => "",
                [first_name] => "Djon",
                [last_name] => "Katview"
        ),
        [1] => Array
            (
                [id] => "181",
                [email] => "",
                [first_name] => "Kavritis",
                [last_name] => "Abrfafda"
        ),
        [2] => Array
            (
                [id] => "182",
                [email] => "",
                [first_name] => "Awaaru",
                [last_name] => "Bhejafry"
        )
    ),
)

我想在两个数组中选择具有相同“id”的元素,并将其附加到新数组,例如“users_final_array”

当它附加到新数组时,我想从两个数组中删除它。

p.s。:不匹配的元素必须保持原样。我差不多3小时就搞乱了它

添加了:

结果数组将如下:

Array
(
    [users_facebook] => Array
    (
    ),

    [users_twitter] => Array
    (
        [0] => Array
            (
                [id] => "181",
                [email] => "",
                [first_name] => "Kavritis",
                [last_name] => "Abrfafda"
            )
    ),

    [users_final_array] => Array
    (
        [0] => Array
            (
                [id] => "180",
                [email] => "",
                [first_name] => "Djon",
                [last_name] => "Katview"
        ),
        [1] => Array
            (
                [id] => "182",
                [email] => "",
                [first_name] => "Awaaru",
                [last_name] => "Bhejafry"
        )
    ),
)

3 个答案:

答案 0 :(得分:2)

通过解码将其转换为json到数组。然后尝试将这两个数组合并为一个数组。合并后使用array_unique()函数删除重复内容如下

array_unique($merge_array);

答案 1 :(得分:0)

首先,这两个数组是JSON数组,所以将主题解码为php的一个:

$array = json_decode($response, true);

和thene,只需使用php“array_merge”并选择它们(facebook和twitter)

答案 2 :(得分:0)

array_intersect

<?php

$res = '{
    "users_facebook": [
        {
            "id": "180",
            "email": "",
            "first_name": "Djon",
            "last_name": "Matfew"
        },
        {
            "id": "182",
            "email": "",
            "first_name": "Awaaru",
            "last_name": "Bhejafry"
        }
    ],
    "users_twitter": [
        {
            "id": "180",
            "email": "",
            "first_name": "Djon",
            "last_name": "Matfew"
        },
        {
            "id": "181",
            "email": "",
            "first_name": "Kavritis",
            "last_name": "Abrfafda"
        },
        {
            "id": "182",
            "email": "",
            "first_name": "Awaaru",
            "last_name": "Bhejafry"
        }
    ]
}';

$res = json_decode($res, true);

print_r(array_intersect($res['users_facebook'],$res['users_twitter']));

?>

array_diff看到原始回复中剩下的内容