Juan Dela
Juan Dela

Reputation: 25

Search item in an array using PHP

I want to search an item like "January" or "February" in an array that looks like this

 Array
(
   [0] => January

   [1] => February

   [2] => March

   [3] => April

)

This what I have tried so far. But not working.

if ( in_array("January", $date_array) ) {
        echo "Found item in Array";
} else {
        echo "Didn't find item in Array";
}

result:

Didn't find item in Array

This is the result of var_dump()

 array(4) {
  [0]=>
  string(9) "January
  "
  [1]=>
  string(10) "February
  "
  [2]=>
  string(7) "March
  "
  [3]=>
  string(7) "April
  "  
  }

Upvotes: 1

Views: 73

Answers (1)

u_mulder
u_mulder

Reputation: 54831

Don't know, where line breaks are coming from, but you can remove them, for example, with array_map:

$date_array = array_map('trim', $date_array);
// and then use `in_array`

Upvotes: 2

Related Questions