I'm looking for a short, built-in way to get a flat array from the values (I'm not interested in the array keys) of a multidimensional array in PHP.
Is there something any built-in function to get an array of elements (which are not an array) from a multidimensional array? I searched for array_flat or similar keywords but found nothing.
Although I'm looking for an efficient way to get a flat array in PHP, what I really would like is to use array_filter in the following: way.
$result = array_filter($array, '!is_array')
Or maybe:
$result = array_filter($array, 'is_array === false')
However, the code above does not work.
The following solutions work, but they are not what I want.
1st solution:
$result = array_diff(array_values($array), array_filter(array_values($array), 'is_array'));
2nd solution:
$result = (array_filter(array_values($args), function($value) {
if(!is_array($value)) {
return $value;
}
}));;
I could also try to use foreach
or array_map
, but I want to write less code.
Why is this? Basically, to help me with file uploads and form validation, I'm building a class called Validator, which will take callbacks (as many as I want) and will execute then. It is possible to pass a callback to run before validation, during validation, and after validation. These are the main callbacks; for each main callback, it is possible to pass an 'onfailure' callback to run if the main callback fails and an 'onsuccess' callback if it succeeds. The Validator class will be pretty flexible and customized regard its arguments when adding a function, so I need to create a slightly complex code to find which argument is which. The arguments may have nested arrays, and that's why I wanted an efficient way to flatten the nested arrays.
The addCallback method of my Validtor Class will be able to receive the following arguments:
'aftervalidation', $callback, $args
['onvalidation', 'callback' => $callback, 'args' => $args]
['onvalidation', [callback => $callback, 'args' => $args], ]
['onvalidation', [$callback, 'args' => $args], ['onsuccess' => $callback, 'args'=> args]]
['aftervalidation', ['callback', 'args' => $args], ['onsuccess' => $callback, $args]]
['beforevalidation', ['callback;, 'args' => $args], ['onsuccess' => $callback,]]
['aftervalidation', [$callback, 'args' => $args], ['onsuccess' => $callback', 'args' => args]]
['onvalidation', ['callback' => $callback, 'args' => $args], ['onsuccess' => $callback', $args]]
['beforevalidation', ['callback' => $callback, $args], ['onsuccess' => $callback']]
['aftervalidation', [$callback, 'args' => $args], 'onsuccess' => [$callback]]
['onvalidation', [$callback, 'args' => $args], 'onsuccess' => [$callback, args]]
['onvalidation', [$callback, 'args' => $args], 'onsuccess' => [$callback, 'args' => $args]]
['onvalidation, [$callback, 'args' => $args], 'onsuccess' => ['callback' => $callback, 'args' => $args]]
Besides these nested arrays, the $args may be an array of args for the callback. So I need to flat these arrays to figure out which arguments is which.
Notes: For personal reasons, to create my custom function for it is not an option.
PHP version is 7.3