比较对象php的2个数组并返回匹配

时间:2018-08-25 10:54:23

标签: php arrays json multidimensional-array

我正在尝试比较两个包含多个动态对象的数组。订单可以随时更改。目标是仅获取匹配的对象。

我已经尝试过功能array_intersect($array1,$array2),但是它不起作用。我找不到合适的数组函数或任何其他解决方案。

我拥有的数据示例

$array1 = '[
        {
            "id": "0",
            "name": "Salary - Daily",
            "charge_type": "2",
            "charge": "20",
            "settlement_channel": "Bank AN Other [1]",
            "account_name": "Jane Doe",
            "account_number": "12345678",
            "id_number": "12345678",
            "phone_number": "12345678",
            "added_by": "John Doe",
            "payment_status": "Payment processed successfully"
        },
        {
            "id": "1",
            "name": "XYZ",
            "charge_type": "10",
            "charge": "30",
            "settlement_channel": "BK Europe [2]",
            "account_name": "Jean Dee",
            "account_number": "6789567845",
            "id_number": "6789567845",
            "phone_number": "6789567845",
            "added_by": "John Doe",
            "payment_status": "Payment processed successfully"
        }
    ]';

$array2 = '[
        {
            "id": "0",
            "name": "Salary - Daily",
            "charge_type": "2",
            "charge": "20",
            "settlement_channel": "Bank AN Other [1]",
            "account_name": "Jane Doe",
            "account_number": "12345678",
            "id_number": "12345678",
            "phone_number": "12345678",
            "added_by": "John Doe",
            "payment_status": "Payment processed successfully"
        },
        {
            "id": "1",
            "name": "NHIF - Daily",
            "charge_type": "2",
            "charge": "30",
            "settlement_channel": "Airtel Money [2]",
            "account_name": "Nicgcgvg",
            "account_number": "98765432345678",
            "id_number": "456787654",
            "phone_number": "09876543",
            "added_by": "Nickson",
            "payment_status": "Payment processed successfully"
        },
        {
            "id": "2",
            "name": "Salary - Daily",
            "charge_type": "2",
            "charge": "50",
            "settlement_channel": "Equitel [4]",
            "account_name": "Nyakambi",
            "account_number": "09876543",
            "id_number": "1234567890",
            "phone_number": "234567890",
            "added_by": "Tom",
            "payment_status": "Payment processed successfully"
        },
        {
            "id": "3",
            "name": "Contribution - Daily",
            "charge_type": "2",
            "charge": "30",
            "settlement_channel": "T-Kash [3]",
            "account_name": "Web App Sol",
            "account_number": "234567890",
            "id_number": "456345678",
            "phone_number": "024567056",
            "added_by": "Tom",
            "payment_status": "Payment processed successfully"
        },
        {
            "id": "4",
            "name": "Staff Commision",
            "charge_type": "2",
            "charge": "50",
            "settlement_channel": "UBA Bank [559900]",
            "account_name": "Nick",
            "account_number": "0006652",
            "id_number": "CPR/00000/2018",
            "phone_number": "568609",
            "added_by": "Tom",
            "payment_status": "Payment processed successfully"
        }
    ]';

预期产量

$matching_objects = '[
            {
                "id": "0",
                "name": "Salary - Daily",
                "charge_type": "2",
                "charge": "20",
                "settlement_channel": "Bank AN Other [1]",
                "account_name": "Jane Doe",
                "account_number": "12345678",
                "id_number": "12345678",
                "phone_number": "12345678",
                "added_by": "John Doe",
                "payment_status": "Payment processed successfully"
            }]';

从匹配中可以看到,我只想在比较两个json数组/字符串后才获得匹配的对象。

1 个答案:

答案 0 :(得分:2)

尝试一下。 这将为您工作。

$array1 = json_decode($array1);
$array2 = json_decode($array2);

$matching_objects = [];

foreach ($array1 as $key1 => $value1) {
    foreach ($array2 as $key2 => $value2) {
        if($value1 == $value2){
            $matching_objects[] = $value2;
        }
    }
}