Problem with array_search

Asked

Viewed 71 times

1

I have the array down that is giving a problem.

When I give a search it only seeks some values it is not finding the 2018-06-11, 2018-06-18 and the 2018-06-26

The rest works perfectly

$hoje = "2018-06-11";
 $diario = array(
    "0.25" => "2018-06-11",
    "0.35" => "2018-06-12",
    "0.38" => "2018-06-13",
    "0.50" => "2018-06-14",
    "0.00" => "2018-06-15",
    "0.25" => "2018-06-18",
    "0.37" => "2018-06-19",
    "0.25" => "2018-06-20",
    "0.23" => "2018-06-21",
    "0.45" => "2018-06-22",
    "0.33" => "2018-06-25",
    "0.28" => "2018-06-26",
    "0.40" => "2018-06-27",
    "0.31" => "2018-06-28",
    "0.28" => "2018-06-29",
    "0.32" => "2018-07-03"
    );


echo $lucrohoje = array_search($hoje, $diario);
  • 4

    The array has equal keys so the last one is the one that prevails. You can see that the array will have less element use the var_dump() or the print_r() to confer.

  • Is that the problem, should it be the other way around maybe? Date as key.

  • 1

    As long as the key doesn’t repeat itself, it’s okay, I don’t know if that’s the case.

  • OK really was the problem, thank you very much

  • It seems to be yes. And it is no longer necessary to use the array_search. Suffice <?php echo $diario[$hoje] ?> (Confirming that the key exists clear)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.