简体   繁体   中英

count array value with condition

I have array data that contain some values. I want to count particular key value.

My Array :

Array
(
    [0] => stdClass Object
        (
            [id] => 3
            [product_id] => 42
            [user_id] => 69
            [order_item_id] => 0
            [sku_id] => 78
            [rate] => 4 // count
            [description] => Wonderful dress.
            [is_certifiedbuyer] => 1
            [status] => 1
            [deleted] => 0
            [created_at] => 2016-03-11 16:53:31
            [updated_at] => 2016-03-11 16:53:31
            [username] => Hiral
            [productname] => Aish dress
        )

    [1] => stdClass Object
        (
            [id] => 4
            [product_id] => 42
            [user_id] => 12
            [order_item_id] => 0
            [sku_id] => 78
            [rate] => 2
            [description] => Greate dress.
            [is_certifiedbuyer] => 1
            [status] => 1
            [deleted] => 0
            [created_at] => 2016-03-11 16:53:31
            [updated_at] => 2016-03-11 16:53:31
            [username] => Admin
            [productname] => Aish dress
        )

)

From above array i want to count total 5 rated user, total 4 rated user, total 3 rated... etc

In short i want to count rate field from above array.

I have tried :

        $reviews=$this->ProductReviewRepo->ProductReview(42);

        $div1 = array_filter($reviews, function($review) {

            return  substr('4', $review->rate) !== false;
        });
        echo '<pre>';print_r(count($div1));
        $div2 = array_filter($reviews, function($review) {

            return substr('4', $review->rate) !== false;
        });
        echo '<pre>';print_r(count($div2));
        $div3 = array_filter($reviews, function($review) {

            return substr('3', $review->rate) !== false;
        });
        echo '<pre>';print_r(count($div3));
        $div4 = array_filter($reviews, function($review) {

            return substr('2', $review->rate) !== false;
        });
        echo '<pre>';print_r(count($div4));
        $div5 = array_filter($reviews, function($review) {

            return substr('1', $review->rate) !== false;
        });

But i get an error of can count only string and integer value.

Convert your object in array and than You can use this:

$arr = array(array('rate'=>4,'id1'=>2),array('rate'=>4,'id1'=>4),
       array('rate'=>3,'id1'=>44));
$ids = array_map(function ($ar) {return $ar['rate'];}, $arr);
$count_array = array_count_values($ids);
print_r($count_array);

Result:

Array ( [4] => 2 [3] => 1 )

This shows that you have 2 users with rating 4 and 1 user with rating 3.

I think this will give you a better performance. Declare the variables $count1 , $count2 ... before the loop. Then keep increment respective variable based on the rating value. I am using $value->rate as they are Objects inside your array.

foreach ($myArray as $key => $value) {
    switch ($value->rate) {
        case '1':
            $count1++;

            break;
        case '2':
            $count2++;

            break;
        case '3':
            $count3++;

            break;
        case '4':
            $count4++;

            break;
        case '5':
            $count5++;

            break;

        default:
            break;
    }
}

WHY BETTER PERFORMANCE

When you use array_filter , it loops over the array. So, when you use many array_filter , you are looping multiple times. But, with this approach you can accomplish it with just one loop over the array.

you can do array_filter by using additional array for count rate like below.But you need to set & for get updated $count.

$count = array(1=>0,2=>0,3=>0,4=>0,5=>0);
array_filter($json, function($review) use (&$count) {   
      if($review->rate == 1){
        $count[1] = $count[1]+1; 
      }
      else if($review->rate == 2){
        $count[2] = $count[2]+1;

      }
      else if($review->rate == 3){
        $count[3] = $count[3]+1;
      }
      else if($review->rate == 4){
        $count[4] = $count[4]+1;
      }
      else if($review->rate == 5){
        $count[5] = $count[5]+1;
      }

    });
var_dump($count);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM