I have the following php code that gets a serialized array from a mysql database, then unserializes it. This is working fine. The following code:
$row=mysql_fetch_array($result);
$mydata=$row[0];
$unser=unserialize($mydata);
echo "$mydata<br>";
print_r($unser);
echo "<br>";
echo $unser[1901];
The output is this:
a:2:{i:2070;s:4:"0.00";i:1901;s:4:"1.00";}
Array ( [2070] => 0.00 [1901] => 1.00 )
1.00
So far, so good. Now, I'm trying to write the code so that it checks if the array key 1901 exists. For that, I tried this:
$search_array = $unser;
if (array_key_exists('1901', $search_array)) {
echo "The key 1901 is in the array";
}
However that is returning an error. What am I doing wrong?
The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.
The function in_array() returns true if an item exists in an array. You can also use the function array_search() to get the key of a specific item in an array. In PHP 5.5 and later you can use array_column() in conjunction with array_search() .
The simplest and fastest way to check if an item is present in an array is by using the Array. indexOf() method. This method searches the array for the given item and returns its index. If no item is found, it returns -1.
PHP in_array() Function The in_array() function searches an array for a specific value. Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.
not found! Use PHP in_array () function to check if a value exists in an array. Did you find this tutorial useful?
PHP array_search() method search an array for given value and return the corresponding key if a value exists in an array. If a value doesn’t exist in an array then it returns NULL . How to sort string using PHP code
Introduction to the PHP in_array () function The in_array () function returns true if a value exists in an array. Here’s the syntax of the in_array () function: in_array (mixed $needle, array $haystack, bool $strict = false) : bool
Approach 1 (Using in_array () method): The array () method can be used to declare an array. The in_array () method in PHP is used to check the presence of an element in the array. The method returns true or false depending on whether the element exists in the array or not.
With the following code:
$mydata= 'a:2:{i:2070;s:4:"0.00";i:1901;s:4:"1.00";}';
$unser=unserialize($mydata);
echo "$mydata<br>";
print_r($unser);
echo "<br>";
echo $unser['1901'];
$search_array = $unser;
if (array_key_exists('1901', $search_array)) {
echo "<br />The key 1901 is in the array";
}
It will work correctly:
a:2:{i:2070;s:4:"0.00";i:1901;s:4:"1.00";}
Array ( [2070] => 0.00 [1901] => 1.00 )
1.00
The key 1901 is in the array
Check if you have more code after the lines of code you have posted. I think is another piece of code which is confusing you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With