I am trying to count the number of elements in the array other than null and compare this number with the number 5. Below is the code

<? $arSelect = Array("ID", "IBLOCK_ID", "NAME", "PROPERTY_DATE_VISIT"); $arFilter = Array("IBLOCK_ID" => "14", "ACTIVE" => "Y", "?PROPERTY_DATE_VISIT" => "04.2018"); $res = CIBlockElement::GetList(Array(), $arFilter, false, Array("nPageSize" => 300), $arSelect); while ($ob = $res->Fetch()) { $result[] = $ob['ID']; } //echo var_dump($result); $value = array(); $peremen = array(); foreach ($result as $value) { $model = new evraz3; $data = $model->loadRestorePass2(1, $value); $firstname = $data["PROPERTY"]["VISITOR"][0]["UF_FIRSTNAME"]; var_dump($firstname); } 

The $ firstname array contains the following: enter image description here

    1 answer 1

    How to count the number of elements in an array other than null

    In your code, honestly, it’s not clear where you are and what you are trying to calculate. But for filtering null (and similar) elements, you can use the array_filter function, and for counting the remaining ones - count .

      $notNullCount = count(array_filter($firstname)); 

    If you want to filter only null values ​​(without 0 , false , '' , etc.), you can either write the appropriate callback function to filter, use array_reduce() , or use a simple loop with comparison.

    • Thank you for your reply, I will try! - Evgeny Pivovarov