[英]How to search for a value in multidimensional array
I have an array that is dynamic.我有一个动态数组。 I want to search if there is any value like "Engraving", then it should return true.我想搜索是否有“雕刻”之类的值,那么它应该返回 true。 How can i implement array search in multidimensional array like below我如何在多维数组中实现数组搜索,如下所示
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
)
)
You can use a recursive function.您可以使用递归函数。
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);
The above function will only match entire values.上面的函数只会匹配整个值。 If you need to match parts of strings, you will need to add a strpos check (Thanks, AbraCadaver) .如果您需要匹配部分字符串,则需要添加 strpos 检查(谢谢,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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.