I am trying to find a particular string in a PHP array as below.
Array ( [0] => Array ( [0] => sku,qty,is_in_stock ) [1] => Array ( [0] => EBCDAA014MAS34B,149.000000,1 ));
I am preparing the above array as below and I am storing it in a variable called $listing
And I am using array_find()
function to do so.
function array_find($needle, array $haystack)
{
foreach ($haystack as $key => $value) {
if (false !== stripos($value, $needle)) {
return $key;
}
}
return false;
}
$hello = array_find('EBCDAA014MAS34B', $listing);
echo $hello;
But still, I am getting the return as false. Can anyone kindly tell me which is the right way to do this?
Your array is an array of arrays where your string is the first item in each of those arrays which will be $value[0]
Try changing this line:
if (false !== stripos($value, $needle)) {
To this line:
if (false !== stripos($value[0], $needle)) {
Simply you can use in_array() PHP Function, see below example.
$listing = Array ( [0] => Array ( [0] => sku,qty,is_in_stock ) [1] => Array ( [0] => EBCDAA014MAS34B,149.000000,1 ));
in_array('EBCDAA014MAS34B', $listing)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.