Php 如何在多维数组中搜索值
我有一个动态数组。我想搜索是否有任何像“雕刻”这样的值,然后它应该返回true。 如何在多维数组中实现数组搜索,如下所示Php 如何在多维数组中搜索值,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有一个动态数组。我想搜索是否有任何像“雕刻”这样的值,然后它应该返回true。 如何在多维数组中实现数组搜索,如下所示 Array ( [0] => Array ( [product_option_id] => 2222 [product_option_value] => Array ( [0] => Array
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
)
)
可以使用递归函数
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);
上述函数将仅匹配整个值。如果需要匹配字符串的某些部分,则需要添加strps检查(谢谢,Abracadver)
这总是将
bool(false)
Change$pineel==$value
返回到strpos($value,$pineel)!==false
哦,是的,这只匹配完整的值。我没有注意到示例文本是部分值@AbraCadaver在与非字符串值进行比较时不会导致问题?仅在'0'
=0
和'1'
=1
等意义上。。。“但这不是问题。”baig772更新了答案。现在应该可以搜索字符串的某些部分了。
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);