2

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

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”

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

ps : 不匹配的元素必须保持原样.. 近 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"
        )
    ),
)
4

3 回答 3

2

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

array_unique($merge_array);
于 2012-05-14T13:56:01.210 回答
0

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

$array = json_decode($response, true);

然后,只需使用 php "array_merge" 并选择它们(facebook 和 twitter)

于 2012-05-14T13:55:44.967 回答
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查看原始响应还剩下什么

于 2012-05-14T14:10:47.517 回答