php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73843 array_search return key is wrong
Submitted: 2016-12-30 09:05 UTC Modified: 2016-12-30 11:17 UTC
From: 376292871 at qq dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 7.0Git-2016-12-30 (Git) OS: win10 64bit
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: 376292871 at qq dot com
New email:
PHP Version: OS:

 

 [2016-12-30 09:05 UTC] 376292871 at qq dot com
Description:
------------
---
From manual page: http://www.php.net/function.array-search
---


Test script:
---------------
$arr = [241, 248, 249, 250, 255, 256, 257];
$except = [248, 249, 255];
foreach ($except as $value) {
    if ($key = array_search($value, $arr) !== false) {
        unset($arr[$key]);
    }
}
print_r($arr);
exit;

Expected result:
----------------
Array
(
    [0] => 241
    [3] => 250
    [5] => 256
    [6] => 257
)


Actual result:
--------------
Array
(
    [0] => 241
    [2] => 249
    [3] => 250
    [4] => 255
    [5] => 256
    [6] => 257
)


Patches

array-search-fix-fix-fix (last revision 2016-12-30 09:12 UTC by 376292871 at qq dot com)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-12-30 10:26 UTC] 376292871 at qq dot com
-Operating System: win7 sp1 64bit +Operating System: win10 64bit
 [2016-12-30 10:26 UTC] 376292871 at qq dot com
update OS
 [2016-12-30 11:17 UTC] laruence@php.net
-Status: Open +Status: Not a bug
 [2016-12-30 11:17 UTC] laruence@php.net
your codes seems wrong:
 if ($key = array_search($value, $arr) !== false)
should be:
 if (($key = array_search($value, $arr)) !== false) {
 [2016-12-30 11:39 UTC] 376292871 at qq dot com
Thank you, Laruence.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Sep 19 05:00:01 2025 UTC