php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #65904 array_keys($array, null) does not behave the same as array_keys($array)
Submitted: 2013-10-15 19:30 UTC Modified: 2013-12-31 20:15 UTC
From: avi at alphapipe dot com Assigned: frozenfire (profile)
Status: Closed Package: Arrays related
PHP Version: 5.4.20 OS: Ubuntu 12.04.3 LTS
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: avi at alphapipe dot com
New email:
PHP Version: OS:

 

 [2013-10-15 19:30 UTC] avi at alphapipe dot com
Description:
------------
According to the documentation at http://php.net/manual/en/function.array-keys.php, the default value for the second parameter in array_keys ($search_value) is null.  However, if you call the function and pass in null as the second value, the function returns an empty array, instead of the array of keys (as it appears to be searching for actual keys that match null).  Shouldn't a function behave the same way if a parameter is left blank or if the default value is passed in?

Test script:
---------------
<?php

$muppetColors = [
    'fozzie' => 'orange',
    'kermit' => 'green'
];

print_r(array_keys($muppetColors));

print_r(array_keys($muppetColors, null));

Expected result:
----------------
Both print_r statements should return arrays [ 'fozzie', 'kermit' ].

Actual result:
--------------
The first print_r statement returns the expected array [ 'fozzie', 'kermit' ], but the second print_r returns an empty array.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-10-15 19:52 UTC] bwoebi@php.net
-Type: Bug +Type: Documentation Problem
 [2013-10-15 19:52 UTC] bwoebi@php.net
Actually this is a documentation problem. array_keys() has no default value for the second parameter. (Reason: You might want to search for keys where value is equal to null.)
 [2013-12-31 19:54 UTC] frozenfire@php.net
-Status: Open +Status: Verified
 [2013-12-31 20:14 UTC] frozenfire@php.net
Automatic comment from SVN on behalf of frozenfire
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=332548
Log: Removed initializer from the search_value parameter, as it was incorrect and confusing.
Fixes bug #65904.
 [2013-12-31 20:15 UTC] frozenfire@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: frozenfire
 [2013-12-31 20:15 UTC] frozenfire@php.net
The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 13:01:32 2024 UTC