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
24
votes
4 answers

Why is array_key_exists 1000x slower than isset on referenced arrays?

I have found that array_key_exists is over 1000x slower than isset at check if a key is set in an array reference. Does anyone that has an understanding of how PHP is implemented explain why this is true? EDIT: I've added another case that seems to…
Kendall Hopkins
  • 43,213
  • 17
  • 66
  • 89
18
votes
4 answers

array_key_exists($key, $array) vs !empty($array[$key])

I've seen a lot of people do the former, is there any performance benefit doing one vs the other? Or is it just an eye candy? I personally use the latter every time as it is shorter and personally more readable to me.
Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
4
votes
3 answers

PHP array_key_exists key LIKE string, is it possible?

I've done a bunch of searching but can't figure this one out. I have an array like this: $array = array(cat => 0, dog => 1); I have a string like this: I like cats. I want to see if the string matches any keys in the array. I try the following but…
Keith C.
  • 365
  • 1
  • 5
  • 15
4
votes
2 answers

PHP array_key_exists does not work; array is not multi-dimensional

I have an array of all the countries in the world as such: $countries = array( "GB" => "United Kingdom", "US" => "United States", "AF" => "Afghanistan", "AL" => "Albania", "DZ" => "Algeria", "AS" => "American Samoa", "AD" =>…
user1057001
3
votes
4 answers

check if a value exists in array

I am trying this code to check if a value exists in an array. $arr = array ('2' => '0', '3' => '0.58'); $num=3; if (array_key_exists($num, $arr)) { echo (array_key_exists($num, $arr)); //show the index, in this case 1 } What i want is show the…
daniel__
  • 11,633
  • 15
  • 64
  • 91
3
votes
2 answers

What is the proper way of checking a table key existence before using its value?

I want to check if $table['key'] exists before using it. What is the proper way of doing this? I've seen a lot of different codes, but I do not know if they are all equivalent and right. Here are a few examples : // 1 if(isset($table['key'])) { ...…
Silverspur
  • 891
  • 1
  • 12
  • 33
3
votes
1 answer

Sum time if date already exists or has a duplicate

I have this array $date = array{"2013-09-17 00:21:00", "2013-09-23 00:12:00", "2013-09-23 00:41:00", "2013-09-20 00:13:00", "2013-09-19 00:34:00", "2013-09-17 00:38:00"} I'm…
bot
  • 4,841
  • 3
  • 42
  • 67
3
votes
5 answers

Find a key exists in the sub keys of an array?

How can I check if a key exists in the sub keys of an array? And if that key of the item is found then return that item? For instance, I have this array, Array ( [0] => Array ( [a] => Array ( …
Run
  • 54,938
  • 169
  • 450
  • 748
2
votes
2 answers

array_key_exists for compound key in array

How do I check to see if a compound key exists with array_key_exists such as $myarr['ind1']['ind2'] Would like to see if the key ['ind1']['ind2'] exists in $myarr. I googled this and looked at some similar answers but couldn't find anything.
macmiller
  • 325
  • 1
  • 4
  • 15
2
votes
4 answers

array_key_exists is not working well

I have this array: $variableNames = [ 'x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7' ]; But, when i use array_key_exists function like this: array_key_exists('x3', $this->variableNames) It…
user6184150
2
votes
1 answer

PHP7: array_key_exists() - something is wrong

I have such problem in PHP 7.0.4, the same code works fine in PHP 5.6.x and older: function array_item(&$array,$key,$default=''){ /* next line has number 1965 in original source */ if(is_array($array) && array_key_exists($key,$array))…
Hink
  • 1,054
  • 1
  • 15
  • 31
2
votes
2 answers

Need to get data value from mysql with array_key_exists

Data fetch from database table for image, in image column we have 8 different sizes image links for one single products and each image link have its size details like:- 100x100, 200x200, 500x500 and so on. All images are specified with…
Pratik Soni
  • 169
  • 2
  • 13
2
votes
1 answer

Array key exists in multidimensional array

I'm trying to arrange a group of pages in to an array and place them depending on their parent id number. If the parent id is 0 I would like it to be placed in the array as an array like so... $get_pages = 'DATABASE QUERY' $sorted =…
PapaSmurf
  • 1,035
  • 2
  • 14
  • 26
1
vote
3 answers

Is (isset(..) || array_key_exists(...)) a faster way of detecting null values than just array_key_exists(...))?

While researching how to detect null values in an array, I came across some user's comment under the http://www.php.net/manual/en/function.array-key-exists.php manual page. It said that if (isset(..) || array_key_exists(...)) { ... } is faster than…
TryHarder
  • 2,704
  • 8
  • 47
  • 65
1
vote
1 answer

array_key_exists() expects parameter 2 to be array, null given - Cakephp 3

Friends, I appreciate any comments. I'm having problems with array_key_exists. I need to check if a product already exists in the shopping cart. I'm doing it this way: . $session = $this->request->getSession(); $cart = $session->read(…
E. Biagi
  • 452
  • 3
  • 12
1
2 3 4 5