Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if value exists in php array - not working?

Tags:

arrays

php

mysql

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?

like image 372
user1227914 Avatar asked Apr 01 '12 19:04

user1227914


People also ask

How do you check if value not exist in array in PHP?

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.

How do you check if value exists in array of objects PHP?

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() .

How do you check if a value exists in an array?

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.

What is in_array function in PHP?

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.

How to check if a value exists in an array?

not found! Use PHP in_array () function to check if a value exists in an array. Did you find this tutorial useful?

How to search an array in PHP?

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

What is in_array () function in PHP?

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

How to declare an array in PHP?

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.


1 Answers

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.

like image 189
Gabriel Santos Avatar answered Oct 12 '22 06:10

Gabriel Santos