Surprisingly array_keys_exist
doesn't exist?! In the interim that leaves some space to figure out a single line expression for this common task. I'm thinking of a shell script or another small program.
Note: each of the following solutions use concise […]
array declaration syntax available in php 5.4+
array_diff+array_keys
if (0 === count(array_diff(['story', 'message', '…'], array_keys($source)))) { // all keys found} else { // not all}
(hat tip to Kim Stacks)
This approach is the most brief I've found. array_diff()
returns an array of items present in argument 1 not present in argument2. Therefore an empty array indicates all keys were found. In php 5.5 you could simplify 0 === count(…)
to be simply empty(…)
.
array_reduce+unset
if (0 === count(array_reduce(array_keys($source), function($in, $key){ unset($in[array_search($key, $in)]); return $in; }, ['story', 'message', '…']))){ // all keys found} else { // not all}
Harder to read, easy to change. array_reduce()
uses a callback to iterate over an array to arrive at a value. By feeding the keys we're interested in the $initial
value of $in
and then removing keys found in source we can expect to end with 0 elements if all keys were found.
The construction is easy to modify since the keys we're interested in fit nicely on the bottom line.
array_filter& in_array
if (2 === count(array_filter(array_keys($source), function($key) { return in_array($key, ['story', 'message']); } ))){ // all keys found} else { // not all}
Simpler to write than the array_reduce
solution but slightly tricker to edit. array_filter
is also an iterative callback that allows you to create a filtered array by returning true (copy item to new array) or false (don't copy) in the callback. The gotchya is that you must change 2
to the number of items you expect.
This can be made more durable but verge's on preposterous readability:
$find = ['story', 'message'];if (count($find) === count(array_filter(array_keys($source), function($key) use ($find) { return in_array($key, $find); }))){ // all keys found} else { // not all}