Check status == 1 or 0 from an array without PHP loop

For a WordPress plugin, I am using a simple $wpdb query like:

global $wpdb;
$table = $wpdb->wp_table_name = $wpdb->prefix . "wp_table_name";

$the_query = $wpdb->get_results(
    "SELECT *
        FROM $table;
    ");

It gives array like (using var_dump()):

Read More
array
  0 => 
    object(stdClass)[342]
       public 'id' => string '1' (length=1)
       public 'name' => string 'The Name' (length=8)
       public 'status' => string '1' (length=1)
  1 => 
    object(stdClass)[341]
       public 'id' => string '2' (length=1)
       public 'name' => string 'The Name 2' (length=10)
       public 'status' => string '0' (length=1)

On the view list I want to show a simple counter like:

Total: 2; Active: 1; Inactive: 1

For the total, I used a simple count():

<?php echo count($the_query); ?>

It’s working fine. Now I want to show the other two numbers, but [if possible] WITHOUT ANY LOOP.

I guess using many loops can slow down the page. I searched many, they are suggesting in_array() or array_key_exists(). But they seems search for the index, they can’t check whether it’s == 1 or 0.

Related posts

Leave a Reply

5 comments

  1. Using a loop in your specific case is a Good solution. You should avoid using loops where it is not necessary but this specific case makes perfect sense.

    You might want to limit the number of results that your query return to a sensible number (like 10 or 15 per page). Looping over 10-15 items will not make much of a difference, specially when you don’t have a better way of getting the end result.

    And of course if you don’t need all the data in the post (like the post’s body) then also limit the number of columns being returned by your SQL or frame a better query based on your requirements.

  2. Agreeing with the other responses, loops aren’t inherently bad. In this case, a loop is probably the simplest way to get what you’re after.

    There are ways to avoid the loop by using a more functional style, such as using array_reduce. eg.

    $active = array_reduce($the_query, function ($result, $item) {
      if ($item->status === '1') {
        return $result + 1;
      }
      return $result;
    });
    
    var_dump($active);
    

    Though, this is still going to perform a loop it’s abstracted away into the underlying C implementation.

    Whether this is a performance improvement or not, I’m not certain (I suspect not). You’d have to profile your code. It’s mostly a stylistic choice.

    Here is a codepad of the example working: http://codepad.viper-7.com/JFzUga

  3. You can utilize the get_var directly instead of get_results

    <?php
    $active_user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users where status=1");
    echo "<p>Active user count is {$active_user_count}</p>";
    
    $inactive_user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users where status=0");
     echo "<p>Inactive user count is {$inactive_user_count}</p>";
    ?>
    

    Hope it helps

  4. Using loops isn’t going to be that tough on your resources. If you are trying to check an array for particular values, you are going to have to use a loop. It’s not like you are parsing big data or anything, so using a loop isn’t going to be that bad. That is what loops are for. What you want to avoid is using nested for loops, or nested loops. For what you are trying to do a loop is fine.

    Loops are a basic building block of coding. What you want to avoid is nesting loops and doing loop after loop within loops. Using a single loop has a runtime of (O)n. When you start nesting loops, you grow that exponentially. A nested for loop will be (O)n^2, 3 nested loops will be (O)n^3 and so on. This is where it becomes bad. So don’t be afraid to use loops with arrays.

    However, one way to do this, if you REALLY don’t want to use loops, would be to do 2 more queries where you could the number of entries that are active/inactive.

    This might look like:

    SELECT COUNT(*) FROM $table WHERE status = 1;
    
    SELECT COUNT(*) FROM $table WHERE status = 0;
    

    Doing this will be more intensive on the resources that using a loop to count, but it’s an option. Personally, I wold just loop over the current array.