Answer the question
In order to leave comments, you need to log in
How to count only nested array elements without parent?
How to count only nested array elements without parent?
How to avoid using loops for this?
I only considered this option.
$array = [
['foo', 'bar'],
['foo', 'bar']
];
$diff = count($array, 1) - count($array);
Answer the question
In order to leave comments, you need to log in
Welcome to php. You have groped for one of his most powerful problems, which no authoritative phpshnik will ever admit.
First on the merits, then on the eternal...
Parent count - count($array);
When you put one as the second parameter in count(), you calculated the sum of the quantities in all elements. Then he subtracted the quantity in the main array. Technically, you got the answer, but the task itself - first to make a pack of packs and then count the number of elements in all packs without taking into account the main pack - this is a bit strange
If you have a pack of packs, it may be useful for you to count the number in each pack. Or the number in the main pack. And if you need the sum in all packs, then it should not be a pack of packs - it should be one pack, otherwise we introduce the concept of "aggregation" - a grouping of internal elements according to a certain attribute, and then any forich and cycles.
But you basically got out of the situation, so I won't say what you're doing wrong. But try to set the task differently for the sake of training.
Here are the functions from my library:
// is_array
// проверяет массив на ключи - цифровые, идущие по порядку с 0
function _is_array($array) : bool
{
if (! is_array($array)) return false;
if (! $array) return true; // empty array is array too
$i = count($array);
while ($i-- && ! array_key_exists($i, $array)) return false;
return true;
}
// is_list
// проверяет является ли массив - списком (list) без буквенных ключей. цифровые ключи в отличие от array могут идти в любом порядке
function _is_list($array) : bool
{
if (! is_array($array)) return false;
if (! $array) return true; // empty list is list too
// contains string key? not a list
foreach (array_keys($array) as $key) {
if (is_string($key)) return false;
}
return true;
}
// is_assoc
// проверяет, является ли массив "грязным" - содержит хотя бы один строковый ключ
function _is_assoc($array) : bool
{
if (! is_array($array)) return false;
if (! $array) return false; // empty assoc is not an assoc
// contains string key? there's assoc
$result = false;
foreach (array_keys($array) as $key) {
if (! is_string($key)) continue;
$result = true;
break;
}
return $result;
}
// is_dict
// проверяет, является ли массив - словарем (dict), без цифровых и пустых ключей
function _is_dict($array) : bool
{
if (! is_array($array)) return false;
if (! $array) return false; // empty dict is not a dict
foreach (array_keys($array) as $key) {
if (is_int($key)) return false; // 0,1,2
if (! $key) return false; // null, ''
}
return true;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question