php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79583 Bug? in array_search
Submitted: 2020-05-11 06:14 UTC Modified: 2020-05-11 06:17 UTC
From: yattyan83 at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 7.3.17 OS: Windows 64bit/Linux aa4240282e4d
Private report: No CVE-ID: None
 [2020-05-11 06:14 UTC] yattyan83 at gmail dot com
Description:
------------
---
From manual page: https://php.net/function.array-search
---
I used array_search() to look at the beginning of the array and find the location of the first null.(In order to fill the array's nulls per request)
But array_search() tests 0 as null.

Try the example below.

Test script:
---------------
$array = [
'a' => 1,
'b' => 0,
'c' => null,
'd' => null
];

echo array_search(null, $array);//expect c


> b

Expected result:
----------------
I would have liked to have gotten a C back if I could.
You can also use foreach as an alternative, so please let me know if this is a spec or a bug.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-05-11 06:17 UTC] requinix@php.net
-Status: Open +Status: Not a bug -Type: Feature/Change Request +Type: Bug
 [2020-05-11 06:17 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

array_search() checks for loose equality by default, and 0 == null.

Use the $strict parameter.
 [2020-05-11 06:38 UTC] yattyan83 at gmail dot com
I see, it was a lack of confirmation on my part.
Using the $strict parameter worked well.
Thank you very much.
 [2020-05-11 07:06 UTC] bugreports at gmail dot com
what about use the third param and set it to TRUE to avoid type juggling
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC