There is an object, it has a bunch of arrays. You need to know if a particular array is empty or not. How to do?

    2 answers 2

    The empty() function is used to determine the "emptiness" of an array.

    What can be done a little better is to create your own function with type hinting , which will work only for arrays, because it introduces additional type safety.

    This can be done, for example, as follows:

     // [a] -> Bool function isArrayEmpty(array $a){ return empty($a); } 
    • one
      "very useful feature" ^^ - Zowie
    • when I do if (empty ($ object ['myarray'])) I write an error: cannot use object of type CIBLOCKresult as array - Tchort
    • you have an error in the code means, by the way, here you are checking for a void an object and not an array of KGBs - Zowie
    • @Tchort Well, you would say right away that this is not a CDBResult of a bitrix. Then use the $x->FieldsCount() method and compare it to zero. - Costantino Rupert
    • @Tchort I’m not sure if you’ll get such performance checks, but most likely not. Just in the event that the sample is made lazily in the bitrix, then, for example, FieldsCount() can load all the results of the query when called. And, for example, you just wanted to know the length :) - Costantino Rupert

    Strange question ... Well, for example ...

     class A { public function __construct() { $this->array = array(); } } $obj = new A(); if(empty($obj->array)) { //если пустой массив обьекта... } 
    • And there is no standard function in php? - Tchort
    • one
      why doesn’t suit you empty ? O_o <br> I gave an example of a class and an empty array for clarity - Zowie