Does anyone know of a way to check the emptiness of an array (rather array elements) without traversing it?

For example, an existing array contains 5 elements and each element has a zero value or non-zero value. Is there a fast way to check if all the elements are zero (or empty) without traversing the array? I cannot use empty() on the array itself as it has elements and therefore not empty.

    hmm, im not sure there is... the only way i could think of doing it would be to return false as soon as i found something that wasnt 0, so

    foreach($array as $a) {
        if($a != 0)
              return false;
    }
    

    why dont you want to traverse the array ? I dont think speed would be an issue... :queasy:

      traversing the array isn't really a problem...the code i have implemented is similar to your suggestion of returning something that says there is a non-zero element in the array...

      i was just curious if there is another way of implementing this with like a built-in function (sorta like empty()) that i am not aware of yet...

      i'm just pretty much trying to get a different perspective on things...

      thanks for your input...

        Hi mobiuz,

        have a look at the function "in_array", the only thing to watch out for is checking for a null value.

        BBK

        🙂

          Look also at [man]array_filter[/man], without a callback function. If the number of elements in what comes out is different from the number of elements in what went in, then some of the elements of what went in must have been empty.

            thanks for the suggestion BigBadKev...but i won't be able to use in_array() as the value i would need to check is a non-specific value greater than zero...

            it seems the only way to do this is to traverse the array and check each element until a non-zero value shows up to determine the "emptiness" of an array (i.e. not all elements have a zero value)...

              i like that approach Weedpacket...it's more direct than traversing the array to check for "empty" elements...

              thanks for the input...

                Write a Reply...