1

I am trying to compare two arrays containing multiple objects that are very dynamic. The order can keep changing from time to time. The goal is to get only matching objects.

I have tried the function array_intersect($array1,$array2) but it not working. I haven't found the right array function or any other solutions to use.

Example of data I have

$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"
        }
    ]';

Expected Output

$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"
            }]';

As you can see from the matching the I want to only get the matching objects after comparing the two json arrays/string.

Nickson
  • 17
  • 5

1 Answers1

2

Try this. This will work for you.

$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;
        }
    }
}