开发者

php - matching elements in an array

开发者 https://www.devze.com 2023-01-31 01:33 出处:网络
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.

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
                            )
                    )
            )
    )


Use array_intersect.

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

codepad example

0

精彩评论

暂无评论...
验证码 换一张
取 消