如何在多维数组中搜索值

时间:2016-05-12 19:42:28

标签: php arrays multidimensional-array

我有一个动态的数组。我想搜索是否有像" Engraving"这样的值,那么它应该返回true。 如何在多维数组中实现数组搜索,如下所示

    Array
(
    [0] => Array
        (
            [product_option_id] => 2222
            [product_option_value] => Array
                (
                    [0] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 8934
                            [option_value_id] => 229
                            [name] => Cherokee
                            [image] => 
                            [price] => $12.95
                            [price_prefix] => +
                        )

                    [1] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 8935
                            [option_value_id] => 228
                            [name] => Narkisim
                            [image] => 
                            [price] => $12.95
                            [price_prefix] => +
                        )

                    [2] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 8933
                            [option_value_id] => 230
                            [name] => Segoe Script
                            [image] => 
                            [price] => $12.95
                            [price_prefix] => +
                        )

                    [3] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 8932
                            [option_value_id] => 227
                            [name] => No Engraving
                            [image] => 
                            [price] => 
                            [price_prefix] => +
                        )

                )

            [option_id] => 57
            [name] => Engraving Fonts
            [type] => select
            [value] => 
            [required] => 0
        )

    [1] => Array
        (
            [product_option_id] => 2221
            [product_option_value] => Array
                (
                )

            [option_id] => 56
            [name] => Engraving Text
            [type] => text
            [value] => 
            [required] => 0
        )

    [2] => Array
        (
            [product_option_id] => 2187
            [product_option_value] => Array
                (
                    [0] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 8855
                            [option_value_id] => 223
                            [name] => Black
                            [image] => 
                            [price] => $4.95
                            [price_prefix] => +
                        )

                    [1] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 8856
                            [option_value_id] => 220
                            [name] => Blue
                            [image] => 
                            [price] => $4.95
                            [price_prefix] => +
                        )

                    [2] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 8857
                            [option_value_id] => 221
                            [name] => Green
                            [image] => 
                            [price] => $4.95
                            [price_prefix] => +
                        )

                    [3] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 8858
                            [option_value_id] => 224
                            [name] => Pink
                            [image] => 
                            [price] => $4.95
                            [price_prefix] => +
                        )

                    [4] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 8859
                            [option_value_id] => 222
                            [name] => Purple
                            [image] => 
                            [price] => $4.95
                            [price_prefix] => +
                        )

                    [5] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 8860
                            [option_value_id] => 225
                            [name] => White
                            [image] => 
                            [price] => $4.95
                            [price_prefix] => +
                        )

                    [6] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 8854
                            [option_value_id] => 226
                            [name] => No Clipboard Booklight
                            [image] => 
                            [price] => 
                            [price_prefix] => +
                        )

                )

            [option_id] => 55
            [name] => Add a Clipboard Booklight 
            [type] => select
            [value] => 
            [required] => 0
        )

    [3] => Array
        (
            [product_option_id] => 1792
            [product_option_value] => Array
                (
                    [0] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 7682
                            [option_value_id] => 71
                            [name] => Blue
                            [image] => http://localhost/myproject/image/cache/catalog/Clipboards/Clipboard_accessories/Calculator_Clips/Calculator_Clip_4-50x50.jpg
                            [price] => $3.75
                            [price_prefix] => +
                        )

                    [1] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 7683
                            [option_value_id] => 70
                            [name] => Green
                            [image] => 
                            [price] => $3.75
                            [price_prefix] => +
                        )

                    [2] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 11178
                            [option_value_id] => 219
                            [name] => Red
                            [image] => 
                            [price] => $3.75
                            [price_prefix] => +
                        )

                    [3] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 11179
                            [option_value_id] => 218
                            [name] => Purple
                            [image] => 
                            [price] => $3.75
                            [price_prefix] => +
                        )

                    [4] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 11180
                            [option_value_id] => 254
                            [name] => Clear
                            [image] => 
                            [price] => $3.75
                            [price_prefix] => +
                        )

                    [5] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 7681
                            [option_value_id] => 84
                            [name] => No Calculator Clip
                            [image] => 
                            [price] => 
                            [price_prefix] => +
                        )

                )

            [option_id] => 19
            [name] => Add a Calculator Clip 
            [type] => select
            [value] => 
            [required] => 0
        )

    [4] => Array
        (
            [product_option_id] => 2328
            [product_option_value] => Array
                (
                    [0] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 9291
                            [option_value_id] => 231
                            [name] => Add 1 Notepad
                            [image] => 
                            [price] => $1.99
                            [price_prefix] => +
                        )

                    [1] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 9292
                            [option_value_id] => 232
                            [name] => Add 2 Notepads
                            [image] => 
                            [price] => $2.99
                            [price_prefix] => +
                        )

                    [2] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 9293
                            [option_value_id] => 234
                            [name] => No Notepad
                            [image] => 
                            [price] => 
                            [price_prefix] => +
                        )

                )

            [option_id] => 58
            [name] => Add a 3.5" H x 10.25" W Notepad
            [type] => select
            [value] => 
            [required] => 0
        )

    [5] => Array
        (
            [product_option_id] => 2186
            [product_option_value] => Array
                (
                    [0] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 8851
                            [option_value_id] => 67
                            [name] => Black
                            [image] => http://localhost/myproject/image/cache/catalog/Clipboards/Clipboard_accessories/Clipboard_Docks/Clipboard_Dock_Colors-50x50.jpg
                            [price] => $9.99
                            [price_prefix] => +
                        )

                    [1] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 8852
                            [option_value_id] => 68
                            [name] => Blue
                            [image] => http://localhost/myproject/image/cache/catalog/Clipboards/Clipboard_accessories/Clipboard_Docks/Clipboard_Dock_Colors-50x50.jpg
                            [price] => $9.99
                            [price_prefix] => +
                        )

                    [2] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 8853
                            [option_value_id] => 69
                            [name] => Red
                            [image] => http://localhost/myproject/image/cache/catalog/Clipboards/Clipboard_accessories/Clipboard_Docks/Clipboard_Dock_Colors-50x50.jpg
                            [price] => $9.99
                            [price_prefix] => +
                        )

                    [3] => Array
                        (
                            [poip_image] => 
                            [product_option_value_id] => 8850
                            [option_value_id] => 79
                            [name] => No Stand
                            [image] => 
                            [price] => 
                            [price_prefix] => +
                        )

                )

            [option_id] => 18
            [name] => Add a Clipboard Stand
            [type] => select
            [value] => 
            [required] => 0
        )

)

1 个答案:

答案 0 :(得分:2)

您可以使用递归函数。

function in_array_recursive($needle, $haystack) {
    foreach ($haystack as $value) {
        if ($needle == $value) return true;  
        if (is_array($value) && in_array_recursive($needle, $value)) return true;
    }
    return false;
}

$found = in_array_recursive('Engraving', $your_array);

以上功能仅匹配整个值。如果您需要匹配部分字符串,则需要添加一个strpos check (Thanks,AbraCadaver)

function in_array_recursive($needle, $haystack) {
    foreach ($haystack as $value) {
        if (is_array($value)) {
            if (in_array_recursive($needle, $value)) return true;
        } else {
            if (strpos($value, $needle) !== false) return true;  
        }
    }
    return false;
}

$found = in_array_recursive('Engraving', $your_array);