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
0
votes
1 answer

PHP array_key_exists not working correctly

I have a php script that is supposed to assign virtual names to ip addresses.
Obed
  • 15
  • 3
0
votes
1 answer

Check if a key has a value in a multidimensional array with php

I put the result of a SQL request in an array to get something like : Array ( [0] => Array ( [id] => 253 [mother_id] => 329 ) [1] => Array ( [id] => 329 [mother_id] => 210 ) [2] => Array ( [id] => 293 [mother_id] => 329 ) [3] => Array ( [id] => 420…
0
votes
2 answers

Two arrays, one with index one without - how to test

I have two arrays. One array is a single record array with no index - it is how all single records are provided by the vendor. The second array (having two or more records contains indexes [0] - [9]. I have a foreach loop which works great on the…
Dak
  • 21
  • 5
0
votes
1 answer

Replacing array_key_exists() with isset() in PHP

Knowing the differences of array_key_exists() and isset() in PHP, I see a lot of advocates on the web suggesting for replacing array_key_exists() with isset(), but I am thinking is it safe to do so? In one project, I have the value of $var submitted…
user2526586
  • 972
  • 2
  • 12
  • 27
0
votes
0 answers

check whether an array contains an index or not php

This is my post data from front end. {"camp": 1, "seg": [{"id":15 "rule":[{"id":152},{"id":153, "NAME":"file2"}]}, {"id":16, "rule":[{"id":152, "NAME":"null"}]} ]} What I need is I do want to check inside the RULE array that It contains an index…
Shiromi
  • 159
  • 1
  • 9
0
votes
1 answer

Check if array inside array exists in php

There is only ever one product returned at one time. When the product has an ingredient, my code works as intended. When it does not, I am getting: Undefined offset: 0 Which references the line of code: if…
Dan
  • 951
  • 1
  • 23
  • 46
0
votes
2 answers

Passing variable by reference creates said variable

function value(&$param){} value($var['key']); echo array_key_exists("key", $var)? "true" : "false"; //true After running this code, $var['key'] ends up existing despite never being explicitly set. This means empty($var) will no longer return true,…
Estecka
  • 388
  • 1
  • 15
0
votes
2 answers

PHP array_key_exists and is not empty

Following on from a previous question, I am now using the following function to check if a key exists in a multi-dimensional array... function array_key_exists_r($needle, $haystack) { $result = array_key_exists($needle, $haystack); …
fightstarr20
  • 11,682
  • 40
  • 154
  • 278
0
votes
2 answers

PHP array_key_exists not working

I have a PHP array that looks like this if I print_r($myarray) Array ( [0] => Array ( [description] => testdecr [link] => testlink [image_id] => 150 ) ) I am trying to check for a key called image_id by doing this... if…
fightstarr20
  • 11,682
  • 40
  • 154
  • 278
0
votes
2 answers

Is it possible to get the key when it is found in array?

Is there a better way to get a key from an array if it is found in case-insensitive search? The logic of the code I need is like below: 1, 'Boo' => 2); if (array_key_exists($search,…
zee
  • 359
  • 3
  • 16
0
votes
1 answer

Check if value exist into two-dimensional tab (array, vb)

I creating tab(x,y) with values: Dim CostCatTab() As Variant Dim tabSize As Long For m = 3 To CCRLastColumn CostCategory = wsSum.Cells(CostCagRow, m).Value Value = wsSum.Cells(CostCagRow, m).Value tabSize = tabSize + 1 ReDim…
4est
  • 3,010
  • 6
  • 41
  • 63
0
votes
2 answers

Returning true on every iteration of array_key_exists() in php

What am I trying to get is.. I do not know check_mobile_exists( $mobiles, $electronics ) this function is returning true all time. I want true when $mobile array key is existed in the $electronics key list and false when not existed. On this result…
Chayan Biswas
  • 566
  • 2
  • 6
  • 18
0
votes
1 answer

php contradictions with $_POST

I have this program: if (!isset($_POST['foo'])) doSomeThing1(); else { if (!array_key_exists('foo',$_POST)) doSomeThing2(); else doSomeThing3(); } but... the program flow goes to the 3d case, failing with the error: undefined index 'foo'…
0
votes
1 answer

PHP - Add Value in a 2D Array if not found

How can I search in a 2d array if a value DOES NOT exist and if not, add the value in the 2d array? I hav been trying using foreach loop however the code adds many values as keys in the array. Now I am trying using array_key_exists but stil not…
SNos
  • 3,430
  • 5
  • 42
  • 92
0
votes
1 answer

array_key_exists throwing error: The second argument should be either an array or an object

I checked all 29 previous posts tagged with array_key_exists and I cannot find a specific question answered that deals with my issue. Our server has recently been updated and we've advanced to PHP 5.2.17 (and yes I know that's still behind, but…
perdrix
  • 23
  • 1
  • 3