2

I've been playing with this all day and haven't figured out a good way to do it...

I have two arrays and am trying to create an array based on matching values.

//$original

    Array
    (
        [0] => Array
            (
                [items] => Array
                    (
                        [0] => Array
                            (
                                [0] => PA
                                [1] => DZ
                                [2] => ER
                                [3] => TY
                            )

                        [1] => Array
                            (
                                [0] => KV
                                [1] => EN
                                [2] => CR
                            )

                        [2] => Array
                            (
                                [0] => HU
                                [1] => GO
                                [2] => GA
                                [3] => FI
                            )
                    )
            )
    )


//$compare    
    Array
    (
        [0] => Array
            (
                [items] => Array
                    (
                        [0] => Array
                            (
                                [0] => PA
                                [1] => AN
                                [2] => ER
                            )

                        [1] => Array
                            (
                                [0] => KV
                            )

                        [2] => Array
                            (
                                [0] => HU
                                [1] => XV
                                [2] => ZL
                                [3] => FI
                            )
                    )
            )
    )

And I'm trying to produce

//$similar
    Array
    (
        [0] => Array
            (
                [items] => Array
                    (
                        [0] => Array
                            (
                                [0] => PA
                                [2] => ER
                            )

                        [1] => Array
                            (
                                [0] => KV
                            )

                        [2] => Array
                            (
                                [0] => HU
                                [3] => FI
                            )
                    )
            )
    )

1 Answer 1

2

Use array_intersect.

$similar = $compare;
$similar[0]['items'] = array_intersect($compare[0]['items'], $original[0]['items']);

codepad example

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.