PHP array_key_exists does not work; array is not multi-dimensional
since you are reading from a file, you may be getting other characters, try trim():
if (array_key_exists(trim($in_data['country_select']), $countries)){
echo "Country Found";
}
else { echo "failed"; }
array_key_exists is not working well
array_key_exists() search for keys not values.
In your first case, you x3
is in value.
So, its not searching.
In this case you can use in_array(), this function searches
for values.
In second case, x3
is key, therefore, searching properly.
PHP array_key_exists not working
"image_id"
key is on nested array under position 0
.
It should be:
...
if (array_key_exists("image_id", $myarray[0]))
PHP array_key_exists not working correctly
array_key_exists
is only for arrays, while the $json
variable contains an object.
Either change $json
to be an array
or use property_exists
for the object.
Example to transform $json into an array
$json = json_decode($read, true);
array_key_exists() not working properly
This should work:
$descriptionArr = array( "uk/page"=>"", "uk/page-two"=>"description of page 2");
function getDescription($uri, $descriptionArr){
if (false !== array_key_exists($uri, $descriptionArr)) {
return $descriptionArr[$uri];
} else {
return false;
}
}
array_key_exists is not working
array_key_exists does NOT work recursive (as Matti Virkkunen already pointed out). Have a look at the PHP manual, there is the following piece of code you can use to perform a recursive search:
<?php
function array_key_exists_r($needle, $haystack)
{
$result = array_key_exists($needle, $haystack);
if ($result) return $result;
foreach ($haystack as $v) {
if (is_array($v)) {
$result = array_key_exists_r($needle, $v);
}
if ($result) return $result;
}
return $result;
}
array_key_exists not working correctly
No, it shouldn't. array_key_exists
checks for the existance of keys, not values. Your $uploadpriv_ass
array's last key is 4, and you're passing the value of 5 to array_key_exists
. Since $uploadpriv_ass[5]
is not set, you're not getting the "T".
array_key_exists not working properly
If i well understand, 'resource_id' doesnt exist all the time
So what you want to test is if it exist in $_POST array :
if(array_key_exists('resource_id', $_POST) == false)
{
$_POST['resource_id'] = 'undefined';
}
PHP in_array and array_key_exists not working for SESSION
You need to change if(!empty($_SESSION["cart_item"]))
to if(isset($_SESSION["cart_item"]))
. Also check for the $_SESSION that is a simple array or not, for matching with in_array
, you need to have array like i assign.
As you mention _since my $SESSION has a multidimensional array, SO make sure your array is a single dimension if you are using in_array
.
let the variables likes this:
$_SESSION["cart_item"] = array('1', '2', '3');
$_GET['code'] = '3';
if(isset($_SESSION["cart_item"])) {
if(in_array($_GET['code'], $_SESSION["cart_item"])){
echo "hiiii";
}else{
echo "byeee";}
}else {
$itemArray = array( $_GET['code'] => array('pcode'=> $_GET['code']) );
$_SESSION["cart_item"] = $itemArray;
}
Result: hiiii
Now its time to use array_key_exists
.
Lets change array:
$_SESSION["cart_item"] = array('1' => array(6, 7, 8), '2' => array(2, 4, 6), '3' => array(1, 5, 9));
if(array_key_exists($_GET['code'], $_SESSION["cart_item"])){
echo "hiiii from key exists";
}else{
echo "byeee";
}
Result: hiiii from key exists
Note: I think you understand how to use and where to use
in_array
andarray_key_exists
.
Related Topics
How to Fix Memory Getting Exhausted with PHPexcel
Pdo + MySQL and Broken Utf-8 Encoding
PHP Warning Permission Denied (13) on Session_Start()
PHP Pdo::Bindparam() Data Types.. How Does It Work
Which Mime Type Should I Use for Mp3
Rounding to Nearest Fraction (Half, Quarter, etc.)
How to Loop Through a MySQL Query via Pdo in PHP
What Is the Best Practice to Export Canvas with High Quality Images
Type Casting for User Defined Objects
Converting a Number Base 10 to Base 62 (A-Za-Z0-9)
Create an Encrypted Zip Archive with PHP
Remove Diacritics from a String