0

I am looking forward to compare two arrays in PHP.

For example, I have array A:

Array
(
    [0] => Array
        (
            [option_id] => 19
            [sub_option_id] => 57
        )

    [1] => Array
        (
            [option_id] => 1093
            [sub_option_id] => 3582
        )

    [2] => Array
        (
            [option_id] => 1093
            [sub_option_id] => 57
        )

)

And array B:

Array
(
    [0] => Array
        (
            [order_option_detail] => Array
                (
                    [0] => Array
                        (
                            [option_id] => 19
                            [sub_option_id] => 57
                        )

                    [1] => Array
                        (
                            [option_id] => 1093
                            [sub_option_id] => 57
                        )

                    [2] => Array
                        (
                            [option_id] => 1093
                            [sub_option_id] => 3582
                        )

                )

        )

    [1] => Array
        (
            [order_option_detail] => Array
                (
                    [0] => Array
                        (
                            [option_id] => 1
                            [sub_option_id] => 2
                        )

                )

        )

)

By looking at the data structure, I can see that array B contains array A. How can I achieve the same analysis using PHP, ie how to check array B contain array A?

Please help me if you know! Thank you so much!

3
  • @EbinManuval array_intersect only works on flat arrays and with a single value. This is a multidimensional array with two values/keys to check. Same thing with array_diff Commented Dec 16, 2018 at 10:35
  • This topic not same issue for my topic. Commented Dec 16, 2018 at 10:53
  • @DavidWinder It work perfectly! Thank u so much! Commented Dec 16, 2018 at 16:48

2 Answers 2

0

You can use the following function for array compare:

function array_equal($a, $b) {
    if (!is_array($a) || !is_array($b) || count($a) != count($b))
        return false;
    $a = array_map("json_encode", $a);
    $b = array_map("json_encode", $b);
    return array_diff($a, $b) === array_diff($b, $a); // mean both the same values
}

And then use it as:

$details = array_column($arrayB, 'order_option_detail');
foreach($details as $detail){ // loop the two items.
    if (array_equal($detail, $arrayA)) {
       // Do what ever
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

From arrayB you only need 'order_option_detail'.

So if we use array_column we can get those isolated.

$details = array_column($arrayB, 'order_option_detail');
foreach($details as $detail){ // loop the two items.
    if($detail === $arrayA){
        // Do something
    }
}

https://3v4l.org/TW670

9 Comments

Thank you, But please see index of array A and array B, they can not same index
That was my mistake. I thought they where the same and I just copied one array to the other. This is the correct output: 3v4l.org/TW670
I changed index of array in this link so it return null.
please help me @Andreas
please check 3v4l.org/fJ4AX @Andreas
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.