PHP比较2个多维数组并获取不匹配的项目

我正在尝试使用“nombre_entrada”、“precio_productor”和“id_funcion”来比较两个多维数组之间的不匹配项。我尝试使用 array_search/array_column 但没有用。


我需要的是获取一个包含不匹配项目的新数组。


数组 1


Array(

    [0] => Array

        (

            [nombre_entrada] => Entrada General

            [precio_productor] => 250

            [id_funcion] => 907

        )


    [1] => Array

        (

            [nombre_entrada] => Entrada General

            [precio_productor] => 300

            [id_funcion] => 907

        )


    [2] => Array

        (

            [nombre_entrada] => Entrada General

            [precio_productor] => 350

            [id_funcion] => 907

        )

    [3] => Array

        (

            [nombre_entrada] => 2 entradas x

            [precio_productor] => 400

            [id_funcion] => 907

        )


)

阵列 2


Array

(

    [0] => Array

        (

            [nombre_entrada] => Entrada General

            [precio_productor] => 350

            [id_funcion] => 907

        )


    [1] => Array

        (

            [nombre_entrada] => 2 entradas x

            [precio_productor] => 400

            [id_funcion] => 907

        )


)

在数组 1 中,索引 0 和 1 不存在于数组 2 中,因此我需要创建一个包含这些项目的最终数组。


最终阵列


Array(

    [0] => Array

        (

            [nombre_entrada] => Entrada General

            [precio_productor] => 250

            [id_funcion] => 907

        )


    [1] => Array

        (

            [nombre_entrada] => Entrada General

            [precio_productor] => 300

            [id_funcion] => 907

        )

)


ITMISS
浏览 259回答 3
3回答

HUWWW

这通常是array_diff.您不能array_diff直接使用for 它,因为它比较数组项的字符串表示以找出差异,并且数组没有不同的字符串表示。(它们都只是转换为“数组”。)PHP 可以比较数组,如果您使用,array_udiff您可以直接在比较函数中比较它们,而无需转换为字符串。$result&nbsp;=&nbsp;array_udiff($array1,&nbsp;$array2,&nbsp;function($a,&nbsp;$b)&nbsp;{&nbsp;return&nbsp;$a&nbsp;<=>&nbsp;$b;&nbsp;});

胡子哥哥

我能够以这种方式创建数组$newArray = array();foreach ($detectadas as $item) {&nbsp; &nbsp; if (&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; array_search($item['nombre_entrada'], array_column($entradasResult2, 'nombre_entrada')) === false OR&nbsp; &nbsp; &nbsp; &nbsp; array_search($item['precio_productor'], array_column($entradasResult2, 'precio_productor')) === false OR&nbsp; &nbsp; &nbsp; &nbsp; array_search($item['id_funcion'], array_column($entradasResult2, 'id_funcion')) === false&nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; array_push( $newArray, $item );&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP