R
R
ragnar_ok2019-08-23 18:18:57
PHP
ragnar_ok, 2019-08-23 18:18:57

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

1 answer(s)
G
Grigory Vasilkov, 2019-08-24
@ragnar_ok

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;
  }

Now an array for you is a pack of something.
A sheet or a list is a pack that has passed through the filters (usually the filtering functions do not change the keys, but simply throw out the extra ones), or it can also be a pack tied to a many-to-many link table.
Assoc is minced this and that. It is desirable to make them less often, but in some places it happens when you need to take off from "some values ​​\u200b\u200bdo not have keys and substitute them in order, and those with keys should be used as is."
Dict is a full-fledged dictionary, a key-value, which is a unit that must be put into an array as a whole and perceived as an integral entity.
Instead of Dict, you can use new StdClass or another object. In general, a dict is a lazy object. Vpadlu to write a class and in general to solve one problem here, I don’t want to - here’s a dict for you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question