dan
dan

Reputation: 1050

php associative array_search odd behaviour

So this is getting on my nerves right now
I have an associative array that I populate from another array:

foreach ($possible_unavailable as $p) {
    $aux[] = array('date' => $p['date'], 'status' => -1);
}
var_dump($aux);

And then I do the following:

foreach ($aux as $pu) {
   var_dump($pu['date']);
   var_dump(array_search($pu['date'], $aux));
}

This is the output:

array(2) {
  [0]=>
  array(2) {
    ["date"]=>
    string(10) "2014-09-01"
    ["status"]=>
    int(-1)
  }
  [1]=>
  array(2) {
    ["date"]=>
    string(10) "2014-09-05"
    ["status"]=>
    int(-1)
  }
}
string(10) "2014-09-01"
bool(false)
string(10) "2014-09-05"
bool(false)


Why "array_search($pu['date'], $aux)" is not returning true?

Upvotes: 0

Views: 88

Answers (1)

danikaze
danikaze

Reputation: 1654

If I understood well, you are trying to search a string in an array filled with arrays, so the array_search is comparing an string with an array like this:

is "2014-09-01" equals to array("date" => "2014-09-01", "status" => -1) ??

Obviously the return value is false.

Upvotes: 1

Related Questions