Core\Arr::contains
Core\Arr::contains(array<T> $haystack, T $needle): boolCore\Arr::contains( array<T> $haystack, T $needle, ): bool
Tests whether an array holds a value.
The result is true as soon as one entry is the value you are looking for, and false when no entry is. The empty array gives false.
The comparison is always exact. There is no second way to compare and no extra argument to pass, so a text is only found by the same text, letter for letter, and "1" never finds the number 1. Numbers are the one thing compared across types: a whole number and the same number written with a fraction are equal, so 1 finds 1.0.
Good to know: this looks at the values, never at the keys. Use Core\Arr::hasKey to test a key, and Core\Arr::keyOf when you need the key of the entry that was found.
Parameters
$haystackarray<T>The array to search.$needleTThe value to look for, compared by identity;int,uint,floatanddecimalare one numeric domain, so1finds1.0.
Return value
Returns bool. true at the first identical entry; false over the empty array.
Examples
Is this tag on the article
<?nvs// Shows how to test whether an array holds a value.
array<string> $tags = ['sale', 'new', 'gift'];
// The value "new" is in the array. This prints yes.echo Core\Arr::contains($tags, 'new') ? 'yes' : 'no', "\n";
// The value "used" is not in the array. This prints no.echo Core\Arr::contains($tags, 'used') ? 'yes' : 'no', "\n";yesnoHow the values are compared
<?nvs// Shows how `Core\Arr::contains` compares a value with the entries. Text is// compared letter for letter. Numbers are compared as numbers.
array<string> $tags = ['sale', 'new'];
// "New" starts with a capital letter, so it is not "new". This prints no.echo Core\Arr::contains($tags, 'New') ? 'yes' : 'no', "\n";
array<float> $prices = [1.0, 2.5];
// 1 and 1.0 are the same number. This prints yes.echo Core\Arr::contains($prices, 1) ? 'yes' : 'no', "\n";
// 3 is not in the array. This prints no.echo Core\Arr::contains($prices, 3) ? 'yes' : 'no', "\n";noyesnoMay this role open the page
<?nvs// Shows how a page checks that the person who opens it has one of the roles// the page allows.
array<string> $allowed = ['editor', 'admin'];array<string> $visitors = ['editor', 'guest'];
foreach ($visitors as string $role) { if (Core\Arr::contains($allowed, $role)) { echo $role, ' may open the page', "\n"; } else { echo $role, ' may not open the page', "\n"; }}editor may open the pageguest may not open the page