0

I have a problem with the output of array_search, with this code, I have to search different ids ($progId) that are located in the column 0 of the multidimentional array $progInLvl . However it wont return a positive result when searching for integer 1, while 2 or 3 are working well.

Do you know where this could be coming from?

Cheers

$progInLvl = [[1, "a", "aaaa"], [2,"b", "bbbb"], [3, "c", "cccc"]];    

$progId = 1;
$newArr = array_column($progInLvl, 0);

if (!array_search($progId, $newArr)){
       $progId = "fail";
    } 

echo $progId;
Clem Mi
  • 66
  • 5
  • `if (false !== array_search($progId, $newArr)){` read that- https://www.php.net/manual/en/function.array-search.php – splash58 Sep 21 '19 at 21:12

1 Answers1

0

array_search() will return the key.

Doing if (!array_search($progId, $newArr)) will evaluate to true if the returned key is 0. 0 is one of the falsy values.

Use this instead :

// triple equal will ensure type and values match
if (array_search($progId, $newArr) === false)
Cid
  • 14,968
  • 4
  • 30
  • 45