-1

Guys please help me figure out with php. In JS i know that i can use .some or .includes, but what is analog of doing it in PHP? I read the docs of php, they are very poor about functions for arrays.

I need to solve next problem:

['banana', 'apple', 'coconut']

i want to return true, if in array above contains any of values from this array ['apple'], order of elements in given array must not matter!

How to solve this problem in php?

WhoIsDT
  • 695
  • 2
  • 9
  • 27

1 Answers1

2

I truly don't feel like PHP documentation is poor when it comes to array functions. In fact the array_interesect documentation is the first Google result for "check if array contains any element of array".

$array_1 = ['banana', 'apple', 'coconut'];
$array_2 = ['banana'];

if (sizeof(array_intersect($array_1, $array_2))) {
    echo "Contains at least 1 element";
} else {
    echo "Doesn't contain even 1 element";
}
Andrea Olivato
  • 2,450
  • 1
  • 18
  • 30
  • 1
    You earned the reputation to [flag posts](https://stackoverflow.com/help/privileges/flag-posts). Please use this privilege on clear duplicate questions such as this one. Basic functionalities such as this one have about 99,99% chance to have already been convered on the site. – El_Vanja Jun 02 '21 at 08:40