php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #13874 isset on a associative array does not distinct if key exist
Submitted: 2001-10-30 11:23 UTC Modified: 2001-10-30 14:09 UTC
From: raymond at bokenkamp dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 4.0.6 OS: ALL
Private report: No CVE-ID: None
 [2001-10-30 11:23 UTC] raymond at bokenkamp dot com
There's no good way to check weather a key exists in an associative array.

The problem is that if the key exists, but the value is null, is treated the
same as if the key doesn't exist. 

The only way I figure is to check: in_array("key", array_keys($array))
but this seams rather expensive.

(Compare: perl has an "exists" function ...)

Sample:

  $sample = array("a" => "a", "b" => NULL);


  print ":".isset($sample['a'])."<br>\n";
  print ":".isset($sample['b'])."<br>\n";
  print ":".isset($sample['c'])."<br>\n";

  print ":".in_array("a", array_keys($sample))."<br>\n";
  print ":".in_array("b", array_keys($sample))."<br>\n";
  print ":".in_array("c", array_keys($sample))."<br>\n";

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-10-30 11:42 UTC] sander@php.net
Not a bug. Intended behaviour. isset() returns false on variables with a NULL value.

Documentation is being worked on.
 [2001-10-30 14:09 UTC] raymond at bokenkamp dot com
understood. However there is still no effective way to check if a key exists in an array !!!
Maybe my title should have been different ... for the real problem see my sample ...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 09:01:30 2024 UTC