php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45420 array_search() finds non-existent element after array_slice()
Submitted: 2008-07-02 18:34 UTC Modified: 2008-07-16 19:51 UTC
From: tomas at matfyz dot cz Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.2.6 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: tomas at matfyz dot cz
New email:
PHP Version: OS:

 

 [2008-07-02 18:34 UTC] tomas at matfyz dot cz
Description:
------------
array_search() function finds non-existent element after array_slice() is used to slice the array.

tested on 5.2.0 and 5.2.5, don't have access to newer system.

Reproduce code:
---------------
$a["nazdar"] = "pozdrav";
$a["write"] = 1;
$a[2] = "ahoj";
$a = array_slice($a, 2);
$keys = array_keys($a);
echo "keys:\n"; var_dump($keys);
check_array_search("wrqwdite2", $keys);
function check_array_search($val, $keys)
{
        $i = array_search($val, $keys);
        if ($i === false) {
                echo "OK\n";
                return;
        }
        echo "BUG!!! Non-existent value \"$val\" found in keys at position $i!!!\n";
}


Expected result:
----------------
keys:
array(1) {
  [0]=>
  int(0)
}
OK

Actual result:
--------------
keys:
array(1) {
  [0]=>
  int(0)
}
BUG!!! Non-existent value "wrqwdite2" found in keys at position 0!!!


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-03 12:50 UTC] florian dot ember at gmail dot com
> array(1) {
>   [0]=>
>   int(0)
> }

0 == 'any non-numeric string'
 [2008-07-03 16:35 UTC] tomas at matfyz dot cz
> 0 == 'any non-numeric string'

Yeees, but how is this related to array_search()? In no way, I believe. Or at least, shouldn't be.
 [2008-07-11 18:43 UTC] felipe@php.net
Do use the third parameter.

http://docs.php.net/array-search
 [2008-07-16 19:51 UTC] tomas at matfyz dot cz
I See.

What I dislike on PHP is that it is ABSOLUTELY CONTRA-INTUITIVE. I know many computer languages but none of them finds zero when I look for "search string".
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 01:01:32 2025 UTC