Questions tagged [array-key-exists]

PHP function that checks if the given key or index exists in the array

The array_key_exists() checks if the given key or index exists in the array.

<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
    echo "1";
}
?>

The output will be

1

66 questions
1
vote
1 answer

How to find string variable and compare with array?

I Have this array: Array ( [0] => bool own = 0 [1] => bool contr_name = 0 [2] => int all_votes = 0 [3] => bool contract_start = 0 [4] => bool contract_end = 0 [11] => clock T [12] => int a [13] => int candi_ID = 1 [14] => int voter_ID =…
Abbas
  • 25
  • 4
1
vote
1 answer

array_key_exists error baffling

This is quite possibly NOT something that can be reproduced through a copy & paste. The issue I am having here is that array_key_exists simply is not working. I have printed my code to the page; it is definitely of type array, not that that is…
Antago
  • 11
  • 4
1
vote
2 answers

array_key_exists not working as expected

Problem I'm having trouble using the PHP function array_key_exists. Even though my array has the key, the function always returns false. I wonder if there is a issue regards using a dynamically growing array. I'm new to PHP, sorry if the question…
web.learner
  • 101
  • 2
  • 8
1
vote
1 answer

Check if a record exists in an array using PDO and Mysql

I'm trying to get a record in my mysql whether a record exists or not in an array. I've searched online about using array_key_exists function and unfortunately it always gives me "Match not found" even though the record exists in the array. This is…
Rukikun
  • 271
  • 3
  • 19
1
vote
1 answer

PHP Undefined index - Isset() is false when index is in array

I'm not able to figure out what the issue is here. I have an associative array with predefined indexes and when trying to access one of the indexes, I'm getting the undefined index error, here's the code, if(!isset($score_value[$index])){ …
Ross
  • 112
  • 1
  • 17
1
vote
1 answer

Dynamically access deep values in a multidimensional array using an array of keys

I want to check if an array key exists within an array. I need to check up to 10 array keys. When using array_key_exists I have to check each array key individually like so... if (array_key_exists('key', $arr) and array_key_exists('key', $arr))…
1
vote
2 answers

Undefined offset despite array_key_exists() check

When using the code below in a function ($floor & $lift are passed in but I've added them here for demonstration) I'm getting an Notice: Undefined offset: 20 on the last return statement. The last return is the mean that's mean to be used in this…
Neil Nand
  • 549
  • 6
  • 25
1
vote
1 answer

array_key_exists after converting from stdClass

I know that it's better to call json_decode with second argument as true if I want to have JSON as array, but PHP allows typecasting stdClass to array, and then this issue happens:
gnysek
  • 319
  • 1
  • 15
1
vote
1 answer

Add one increment if same numeric array key exists

I have a following array and i need to sort this array based upon the key value. I wrote a code and got perfect result if all numeric keys are different. $myarray_need_to_sort =Array ( [13] => 214 [14] => 215 [15] => 216 [21] =>…
RD Mage
  • 103
  • 3
  • 16
1
vote
2 answers

How to find key from value

I've got 2 arrays $amen=Array ( [0] => 1 [1] => 5 [2] => 2 [3] => 8 [4] => 9 [5] => 7) $amenArr=Array ( [1] => Array ( [en] => Air conditioning [th] => เครื่องปรับอากาศ ) [5] => Array ( [en] => Balcony/terrace [th] => ระเบียง/ทางเดิน ) [2] =>…
Wilf
  • 2,297
  • 5
  • 38
  • 82
1
vote
2 answers

Are there any way to check exitency of an array key inside the function

I have a project that I need to echo $variable['key']; Some times $variable['key'] is not exist. And Those variables creates errors when I echo $variable['key']; directly. My current method is echo (isset($variable['key'])) ? $variable['key'] :…
Deniz Porsuk
  • 492
  • 1
  • 6
  • 20
1
vote
3 answers

array_key_exists contains string

I am using array_key_exists on an associative array to detect if a key exists like this... if (array_key_exists('Packaged price (£3.00)', $item)) { echo 'The Key Exists'; } This works fine, but I want to modify it so that it checks if the key has…
fightstarr20
  • 11,682
  • 40
  • 154
  • 278
1
vote
1 answer

array key and value matter

I have an array $fping and when its key is equal to a variable($probe_name), how to get the exact value from that array. $fping=array( 'FPing1'=>$x1, 'FPing3'=>$x3, 'FPing5'=>$x5, 'FPing10'=>$x10, 'FPing30'=>$x30, …
Thusitha Sumanadasa
  • 1,669
  • 2
  • 22
  • 30
1
vote
1 answer

PHP array_key_exists returning FALSE incorrectly

My data is being pulled from a client's CMS, but I'm getting strange results print_r($appliance_data); foreach ($appliance_data as $adKey => $adValue) { print_r($adKey); print_r($adValue); print_r(array_key_exists($adKey,…
Zeophlite
  • 1,607
  • 3
  • 19
  • 36
0
votes
1 answer

Count specific values of PHP Array that has no column assignments

I need to display a count of the no of car makes. $array = array( array(1,'Benz'), array(3,'Toyota'), array(4,'Mazda'), [2,'VW'], [5,'Mazda'] ); $count =…
user389391
  • 97
  • 1
  • 8