php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #15468 problem with array_key_exists
Submitted: 2002-02-09 13:20 UTC Modified: 2002-02-09 14:36 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: paja at onride dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.1.1 OS: Linux d2 2.2.20-ow1-procmax-smp
Private report: No CVE-ID: None
 [2002-02-09 13:20 UTC] paja at onride dot com
Hello,

I'm trying function array_key_exists in this sample script, but doesn't work:

<?
$a="ap";
$pole = array(1 => "ap", "aa", "dd");
if (array_key_exists($a, $pole)):
    echo "The element is in the array";
endif;
?>

but when using:

$pole = array("ap" => 1, "aa" => 2, "dd" => 3);

it's working. Can somebody look at this problem?

Thanks,
Pavel Hrabal

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-02-09 13:39 UTC] mfischer@php.net
Simple because 'ap' is NOT a key in the first case. See yourself with var_dump($pole); how the array looks like.
 [2002-02-09 13:55 UTC] paja at onride dot com
first case:

array(3) { [1]=> string(2) "ap" [2]=> string(2) "aa" [3]=> string(2) "dd" } 


second case:

array(3) { ["ap"]=> int(1) ["aa"]=> int(2) ["dd"]=> int(3) } 

Why is this different? Shouldn't this be the same?

as Array() definition says: 

Syntax "index => values", separated by commas, define index and values. index may be of type string or numeric. When index is omitted, a integer index is automatically generated, starting at 0. If index is an integer, next generated index will be the biggest integer index + 1. Note that when two identical index are defined, the last overwrite the first.

So I have used in first case the Example 3. 1-based index with array():

$firstquarter  = array(1 => 'January', 'February', 'March');

I thing, that the second case definition is wrong and should return error, but as I see, the function array_key_exists works with this... I'm I mad or what???
 [2002-02-09 14:14 UTC] paja at onride dot com
for mfischer@php.net: Can you explain me your words:

Simple because 'ap' is NOT a key in the first case.

?????

Just don't know, what you mean...
 [2002-02-09 14:31 UTC] cnewbill@php.net
$pole = array(1 => "ap", "aa", "dd");
              |
              |
             Key

$pole = array("ap" => 1, "aa" => 2, "dd" => 3);
               |          |          |
               |          |          |
              Key        Key        Key

This is what he means, ap is NOT a KEY in the first array it is a VALUE.

The function you want is in_array() - http://www.php.net/manual/en/function.in-array.php

-Chris

 [2002-02-09 14:36 UTC] paja at onride dot com
Got it, thanks a lot. One hint - maybe someone should add to manual, what is key and what is it not....
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed Dec 11 19:01:25 2019 UTC