John Davidson

arrays - PHP in_array function doesn't find value

0 comments
Message:


I have this PHP variable:


$values = $response->getValues();

That print this array:


Array ( [0] => Array ( [0] => 16777439-3 ) [1] => Array ( [0] => 17425847-3 ) )

Then I have this code to find a value inside of that array:


if (in_array("16777439-3", $values)) {

echo "Is in array";

}else{

echo "Isn't in array";

}

But all the time it returns "Isn't in array"


Also I've tried to convert my original variable with:


$array2 = json_decode(json_encode($values), true);

But also I'm getting the same return.



My Answer:
The `in_array()` function in PHP is used to check if a value exists in an array. It takes two parameters: the value to search for and the array to search in.

Here's an example of how to use the `in_array()` function:

php
$fruits = array("apple", "banana", "orange", "grape");

if (in_array("banana", $fruits)) {
echo "Found banana in the array";
} else {
echo "Banana not found in the array";
}


In this example, the `in_array()` function is used to check if the value "banana" exists in the `$fruits` array. If it does, it will output "Found banana in the array", otherwise it will output "Banana not found in the array".

Rate this post

5 of 5 based on 4403 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us