I would have a rather simple question today. We have the following resource:
$a = array(1 => 5, 2 => 3, 3 => 13, 9 => array('test'), 4 => 32, 5 => 33);
How is it actually possible to find out if array "a" has an array element in it and return the key if there is one (or more)?
One possible approach:
function look_for_array(array $test_var) {
foreach ($test_var as $key => $el) {
if (is_array($el)) {
return $key;
}
}
return null;
}
It's rather trivial to convert this function into collecting all such keys:
function look_for_all_arrays(array $test_var) {
$keys = [];
foreach ($test_var as $key => $el) {
if (is_array($el)) {
$keys[] = $key;
}
}
return $keys;
}
Demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With