搜索数组中的值不起作用
问题描述:
One of my array value contains
$all_data_array = Array
(
[0] => 'General Information'
[1] => 'Brand'
[2] => '<p><div style="bolor:#000000;"><li>Product Details</li></div></p>'
)
.
And I want to search for key of the value in Array, where array value has text "Product Details" in it, wrapped around some html data. I wrote the following code, but it's not working. Can someone help?
<?php echo $new_key = array_search('Product Details', $all_data_array); ?>
答
array_search
need the string value be exactly the same.
You could just use a loop in your case.
foreach ($all_data_array as $key => $value) {
if (strpos($value, 'Product Details') !== false) {
$new_key = $key;
break;
}
}
答
array_filter
lets you specify a custom function to do the searching. In your case, a simple function that uses strpos()
to check if your search string is present:
答
array_search, looks for the exact match, third element of array $all_data_array has other characters(Here html tags) apart from "Product Details";