如何检查数组是否只包含PHP中的其他数组?

I'm looking for a simple way/function to tell me if an array contains only other arrays or if there are values contained as well. For example,

$a = array(array(), array(), array())

should end up with a 'true' result but

$b = array(array(), 1, 17)

should end up with a 'false result.

I know I can do the following,

function isArrayOfArrays($a) {
    foreach ($a as $value) {
        if (!is_array($value))
            return false;
    }
    return true;
}

I'm wondering if there is a more elegant way.

You can try with array_filter:

$isArrayOfArrays = empty( array_filter($b, function($item){
  return !is_array($item);
}) );

Or even shorter:

$isArrayOfArrays = array_filter($b, 'is_array') === $b;

You could also try array_map with is_array, and see if all elements are true with in_array:

print_r(!in_array(0, array_map(is_array, $a)));

Basically, is_array is performed on each element of $a, so you get an array of booleans. Then you check if this new array is all true.

I'm fond of higher-order functions and the map-reduce paradigm:

/**
 * && as a function.
 *
 * This is our reducer.
 */
function andf($a, $b)
{
    return $a && $b;
}

function isArrayOfArrays($a)
{
    if (!is_array($a)) {
        return false;
    }

    return array_reduce(array_map('is_array', $a), 'andf', true);
}

This has slightly different semantics from your version, but I argue they are more correct (if it's not an array in the first place, it can hardly be an array of arrays can it?).

This formulation is arguably more elegant, but the difference is so small I would probably use the loop in actual code.