A possible technique would be to use count
to count the values of array
and then compare with count
using the second parameter COUNT_RECURSIVE
.
But this method has a flaw. If array
is empty, we will have an unexpected return:
$a = [1, 2]
$b = [1, []]
count($a) == count($a, COUNT_RECUSIVE); // true, não é multidimensional
count($b) == count($b, COUNT_RECUSIVE); // true, mas está errado, pois pelo fato de o 'array' está vazio, não conta recursivamente.
So what's the solution?
I thought of using the gettype
function combined with array_map
to circumvent the situation:
in_array("array", array_map('gettype', $a));
in_array("array", array_map('gettype', $b));
The gettype
function will return the type name of the passed value. Combining with array_map
, it will use this function on each element of array
, returning a array
of variable type names.
The only solution is to check that the word "array"
is within that array
generated. If so, it is multidimensional.
For performance issues shown in the PHP manual itself regarding gettype
function, we can replace it with is_array
. But we would have to check if true
exists in the new array
generated by array_map
.
in_array(true, array_map('is_array', $b), true)