Bug #73843 [Com]: array_search return key is wrong

From: Date: Fri, 30 Dec 2016 11:39:56 +0000
Subject: Bug #73843 [Com]: array_search return key is wrong
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-206239@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=73843&edit=1 ID: 73843 Comment by: 376292871 at qq dot com Reported by: 376292871 at qq dot com Summary: array_search return key is wrong Status: Not a bug Type: Bug Package: Arrays related Operating System: win10 64bit PHP Version: 7.0Git-2016-12-30 (Git) Block user comment: N Private report: N New Comment: Thank you, Laruence. Previous Comments: ------------------------------------------------------------------------ [2016-12-30 11:17:34] 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 10:26:21] 376292871 at qq dot com update OS ------------------------------------------------------------------------ [2016-12-30 09:05:52] 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 ) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=73843&edit=1

« previous php.bugs (#206239) next »