-1

I made an array of data that has stored rows from my database, i need to check if the array contains a specific word from it, so i made

if($data_posts[0] == 'Payday') return echo 'this is a payday.';

But i am kinda struggling on this one since i am not an advanced PHP developer. Could someone enlighten me and show me how could i move on this?

Also tried, but with no success.

if($data_posts[0] == "Payday"){ // if the array contains the word payday.
    echo'This is a payday.';
}else{ // if not
    echo'This is not a payday.';
}
fedefomin
  • 19
  • 3
  • 1
    Check out [in_array](https://www.php.net/manual/en/function.in-array.php) – DarkBee Sep 28 '20 at 12:25
  • What you are doing there is just checking if the first element of array is "Payday". https://www.php.net/manual/en/function.in-array.php is what you need – Gabriel Alejandro López López Sep 28 '20 at 12:31
  • as far as i understand in the docs the in_array() function is checking for the word in all the elements of the given array , while if i do ```if (in_array('Payday', $data_posts[0]))``` an error gets thrown and says the "second parameter expects to be an array" – fedefomin Sep 28 '20 at 12:34
  • You can use array_search(search element, your array). – Rabby Sep 28 '20 at 12:38
  • Are you looking for an _exact_ match here, or are you trying to determine whether a longer text string _contains_ the word at some position? Both are two very different things. – 04FS Sep 28 '20 at 12:40
  • `$data_posts[0]` contains what? – user3783243 Sep 28 '20 at 12:41
  • @04FS As you said , yes. trying to determine whether a longer text string contains the word at any position. – fedefomin Sep 28 '20 at 12:46
  • 1
    Does this answer your question? [How do I check if a string contains a specific word?](https://stackoverflow.com/questions/4366730/how-do-i-check-if-a-string-contains-a-specific-word) – 04FS Sep 28 '20 at 12:47
  • @04FS yes.! Thank you. Much appreciated. – fedefomin Sep 28 '20 at 12:49

1 Answers1

0

You have to use in_array() passing you array as parameter. Using

in_array('Payday', $data_posts[0])

you are calling the function on a single object.

Try calling

in_array('Payday', $data_posts)

Agnohendrix
  • 340
  • 1
  • 6
  • 12