php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39922 array_search returns incorrect position after array_push or array_unshift
Submitted: 2006-12-21 11:40 UTC Modified: 2006-12-22 12:31 UTC
From: willem at fkkc dot nl Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.0 OS: Debian
Private report: No CVE-ID: None
 [2006-12-21 11:40 UTC] willem at fkkc dot nl
Description:
------------
array_search returns incorrect position-value when an array is modified with array_push or array_unshift

Reproduce code:
---------------
$array = array('a1','b2','c3');
array_unshift($array,'d4');
$pos = array_search('d4');
if($pos === FALSE){
    print implode(',',$array);
}else{
    print 'Position = '.$pos;
}

Expected result:
----------------
Position = 0

Actual result:
--------------
d4,a1,b2,c3

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-12-21 13:51 UTC] smlerman at gmail dot com
Fix your error_reporting and/or display_errors settings.

Warning: Wrong parameter count for array_search() in ... on line 4

http://www.php.net/manual/en/function.array-search.php
 [2006-12-21 14:31 UTC] tony2001@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.


 [2006-12-22 11:09 UTC] willem at fkkc dot nl
I'm sorry for the typo in the reproduce-code, here's a correct one.

$array = array('a1','b2','c3');
array_unshift($array,'d4');
$pos = array_search('d4',$array);
if($pos === FALSE){
    print implode(',',$array);
}else{
    print 'Position = '.$pos;
}

I've re-opened the bug-report because it's not a bogus to my opninion.

The code worked fine in php 5.1.4, in php 5.2 not anymore.
The only work-around to fix the problem was to implode the array and use regular expressions to check if the value is there.
 [2006-12-22 12:31 UTC] tony2001@php.net
5.2.0 - Position = 0
5.1.6 - Position = 0
5.0.3 - Position = 0
4.4.4 - Position = 0

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun May 11 17:01:27 2025 UTC