A possible technique would be using count
to count the values of array
and then compare with count
using the second parameter COUNT_RECURSIVE
.
Only this method has a flaw. If the array
have 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 I’d use the function gettype
combined with array_map
to get around the situation:
in_array("array", array_map('gettype', $a));
in_array("array", array_map('gettype', $b));
The function gettype
return the name of the type of the value passed. Matching array_map
, it will use this function in each element of the array
, returning a array
of variable type names.
The solution is just to check whether the word "array"
is inside that array
generated. If yes, it is multidimensional.
For performance reasons shown in the PHP manual itself regarding the function gettype
, we can replace it with is_array
. But we would have to check whether true
exists in the new array
generated by array_map
.
in_array(true, array_map('is_array', $b), true)
I think it’s that that search. I already needed that check, fell like a glove.
– Papa Charlie
@Papacharlie this I know :D. Only she has a small flaw. If the
array
internal is empty, it will recognize that the value ofcount
is equal– Wallace Maxters
I couldn’t get around with a
array filter
? I can’t remember if I ever used the filter, then I’ll check the function.– Papa Charlie