I have an array like this and i m trying to search a value using this array:
$arr = Array('Slovenly Mexico','Slovenly Europe','Greece');
I am using such kind of condition. please help
if($a[storename] == $arr[$i]){
//some code
}
but it is not working because it searches as index value so it gives an error.
Array
(
[0] => Array
(
[storename] => Greece
)
[1] => Array
(
[storename] => Slovenly Europe
)
[3] => Array
(
[storename] => Slovenly Europe
)
[4] => Array
(
[storename] => Greece
)
[5] => Array
(
[storename] => Slovenly Mexico
)
[6] => Array
(
[storename] => Slovenly Europe
)
[7] => Array
(
[storename] => Slovenly Mexico
)
}
you can use in_array()
eg.,
$sizeofarray=sizeof($a);
for($i=0;$i<sizeofarray;$i++)
{
if(in_array($a[$i][storename],$arr))
{
echo "Found";
break;
}
else
echo "Not found";
}
have a look at this function:
http://php.net/manual/en/function.array-search.php
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
?>
Try this
in_array($a[storename], $arr)
you can search value using array bu
A.
in_array
— Checks if a value exists in an array .in_array
return Boolean value
<?php
$os = array("one", "two", "three", "four");
if (in_array("two", $os)) {
echo "Got two";
}
if (in_array("one", $os)) {
echo "Got one";
}
?>
B. you can also use
array_search
— Searches the array for a given value and returns the corresponding key if successful
<?php
$array = array(0 => 'one', 1 => 'two', 2 => 'three', 3 => 'four');
$key = array_search('one', $array); // $key = 0;
$key = array_search('four', $array); // $key = 3;
?>
for further reference array_search and in_array