Home php What is the best way to check that there are no elements...

What is the best way to check that there are no elements in an array?

Author

Date

Category

Which of the options is the most correct for checking a php array for the absence of elements in it?

  1. if ($ arr) {...}
  2. if (empty ($ arr)) {...}
  3. if (isset ($ arr [0])) {...}
  4. if (count ($ arr)) {...}

  5. Answer 1, authority 100%

    Yes, all, in general, are correct. Only in the first and fourth cases there will be a type conversion to bool, and the third is suitable only for indexed arrays. There is another option sizeof ($ arr) .


    Answer 2, authority 19%

    Perhaps the best option is if ($ arr === Array ()) {...}


    Answer 3, authority 12%

    ANSWER :

    Checking an empty array using the count and sizeof functions is not worth it, as it wastes time on processing. scanty time, but wasting.

    The best options are:

    1. if ($ arr) {...} // Check for elements inside
    2.if (empty ($ arr)) {...} // Check the existence of a variable with an array and the presence of elements inside
    3.if (array_shift ($ arr)) {...} // Check if the first element is present
    4. if ($ arr [0]) {...} // Check for the presence of a zero element, for an index array that starts from 0
    

    RESEARCH:

    1. This answer is verified through a complex array microtest with 40014 elements and PHP 7.1
    2. The script was executed on the local machine (Open Server 5.2).
    3. Metering took place through the php microtime () function
    4. The result was checked three times

    STUDY RESULTS:

    First launch

    1. if (count ($ arr)) {…}: 0.0010001659393311 sec.
    2. if (sizeof ($ arr)) {…}: 0 sec.
    3. if ($ array) {…}: 0 sec.
    4. if (empty ($ arr)) {…}: 0 sec.
    5. if (array_shift ($ arr)) {…}: 0 sec.
    6. if ($ arr [0]) {…}: 0 sec.

    Second launch

    1. if (count ($ arr)) {…}: 0 sec.
    2. if (sizeof ($ arr)) {…}: 0.0010011196136475 sec.
    3. if ($ array) {…}: 0 sec.
    4. if (empty ($ arr)) {…}: 0 sec.
    5. if (array_shift ($ arr)) {…}: 0 sec.
    6. if ($ arr [0]) {…}: 0 sec.

    Third run

    1. if (count ($ arr)) {…}: 0.00099992752075195 sec.
    2. if (sizeof ($ arr)) {…}: 0 sec.
    3. if ($ array) {…}: 0 sec.
    4. if (empty ($ arr)) {…}: 0 sec.
    5. if (array_shift ($ arr)) {…}: 0 sec.
    6. if ($ arr [0]) {…}: 0 sec.

    WHAT I DO:

    For myself I use if ($ arr) {…} and if ($ arr [0]) {…}, since there is no need to call additional functions – wrappers


    Answer 4, authority 6%

    if ($ array === []) {

    }

    More optimal because:

    • The code is more readable – “If the variable is equal to an empty array”;
    • Type checking is in progress and you are actually comparing the array;
    • No functions are used, only comparison, which means this code will work a little faster;

    Answer 5

    I am using if ((bool) array_filter ($ array))


    Answer 6

    The documentation says that a array with no elements is interpreted as false .
    https: //www.php. net / manual / en / language.types.boolean.php # language.types.boolean.casting

    var_dump ((bool) array (12)); // bool (true)
    var_dump ((bool) array ()); // bool (false)
    

    So I made sure that the fastest way

    if ($ arr) {}
    
    $ arr = [];
    $ a1000 = range (0, 1000); // 0,1,2 ... 1000
    for ($ i = 0; $ i & lt; 1000000; $ i ++) {
      $ j = rand (0.20);
      if ($ j === 0) {
        $ arr [] = [];
      } else {
        shuffle ($ a1000);
        $ arr [] = array_slice ($ a1000,0, $ j);
      }
    }
    $ t = microtime (true);
    $ n = count_not_empty1 ($ arr);
    $ t = microtime (true) - $ t;
    echo 'if ($ a): N ='. $ n. ' T = '. Round ($ t * 1000,4).' ms'.PHP_EOL;
    $ t = microtime (true);
    $ n = count_not_empty2 ($ arr);
    $ t = microtime (true) - $ t;
    echo 'if ($ a! == []): N ='. $ n. ' T = '. Round ($ t * 1000,4).' ms'.PHP_EOL;
    $ t = microtime (true);
    $ n = count_not_empty3 ($ arr);
    $ t = microtime (true) - $ t;
    echo 'if (! empty ($ a)): N ='. $ n. ' T = '. Round ($ t * 1000,4).' ms'.PHP_EOL;
    $ t = microtime (true);
    $ n = count_not_empty4 ($ arr);
    $ t = microtime (true) - $ t;
    echo 'if (count ($ a)): N ='. $ n. ' T = '. Round ($ t * 1000,4).' ms'.PHP_EOL;
    function count_not_empty1 (& amp; $ arr)
    {
      $ n = 0;
      for ($ i = 0; $ i & lt; 1000000; $ i ++) {
        if ($ arr [$ i]) {
          $ n ++;
        }
      }
      return $ n;
    }
    function count_not_empty2 (& amp; $ arr)
    {
      $ n = 0;
      for ($ i = 0; $ i & lt; 1000000; $ i ++) {
        if ($ arr [$ i]! == []) {
          $ n ++;
        }
      }
      return $ n;
    }
    function count_not_empty3 (& amp; $ arr)
    {
      $ n = 0;
      for ($ i = 0; $ i & lt; 1000000; $ i ++) {
        if (! empty ($ arr [$ i])) {
          $ n ++;
        }
      }
      return $ n;
    }
    function count_not_empty4 (& amp; $ arr)
    {
      $ n = 0;
    for ($ i = 0; $ i & lt; 1000000; $ i ++) {
        if (count ($ arr [$ i])) {
          $ n ++;
        }
      }
      return $ n;
    }
    
    if ($ a): N = 952522 T = 117.7402 ms
    if ($ a! == []): N = 952522 T = 122.844 ms
    if (! empty ($ a)): N = 952522 T = 125.299 ms
    if (count ($ a)): N = 952522 T = 128.9139 ms
    

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions