Reputation: 67
I want to Search in Array value by in_array Function and For loop. My code:
$input = "a";
$arrays = array("cdf","abs","tgf");
$counter = count($arrays);
for ($i=0; $i<$counter; $i++){
if(in_array($input,$arrays) !== true){
echo "Found <br>";
} else {
echo "Not Found";
}
}
Output:
Not Found
Found
Not Found
But, if(in_array($input,$arrays[$i]) !== true)
not working.
Upvotes: 0
Views: 44
Reputation: 16071
The reason in_array("a", "cdf")
, which is what in_array($input, $arrays[$i])
could become, isn't working is because "cdf"
isn't an array.
Are you trying to find array elements in $arrays
that contain the letter a
?
In that case you should search array elements with strpos()
to determine if a string contains another string. You can also use foreach
instead of for
if iterating over the array is all you want to do.
$input = "a";
$arrays = array("cdf","abs","tgf");
foreach ($arrays as $key => $value)
{
if (strpos($value, $input) !== false)
echo "Found in $key<br>";
else
echo "Not Found<br>";
}
Upvotes: 2