<?php
function createValidator($key, $value) {
return fn ($x) => is_array($x) && $x[$key] === $value;
}
$x = [
123 => ['name' => 'xxx'],
456 => ['name' => 'yyy'],
];
echo \array_key_exists(123, $x) ? 'yes' : 'no';
echo "\n";
$x = [
['id' => 123, 'name' => 'xxx'],
['id' => 456, 'name' => 'yyy'],
];
$y = \array_filter($x, createValidator('id', 123));
echo \count($y) ? 'yes' : 'no';
// if array child id === 123 in any of the children,
// then echo yes,
// (without using a loop, and by using an array function)
- Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- yes
yes
preferences:
85.41 ms | 402 KiB | 60 Q