简体   繁体   中英

how to get count based on id then pass to some variable in php?

i have some problem about count object with some value. Here is the data


$data = [
    {
        order_id: "131",
        order_status: "3",
    },
    {
        order_id: "130",
        order_status: "8",
    },
    {
        order_id: "129",
        order_status: "8",
    },
    {
        order_id: "128",
        order_status: "6",
    },
    {
        order_id: "127",
        order_status: "6",
    }
],

i want to count how many "order_status == 3/6/8" then i want to pass into some variable like

if($order_status == '3') 
{
 $value = 'Accepted'; 
}
elseif ($order_status == 6) 
{
 $value = 'Delivered';
}
else {
 $value = 'Finished';
}

then the final result is like

Accepted = 1
Delivered = 2
Finished = 2

and from what i do is like


        $count = count($data);
        for ($i = 0; $i < $count; $i++) {
            $x = array();
            foreach($data as $row) {
                $x[$i] = $data[$i]->order_status;
            }

            $z[] = $x;


        }
        echo print_r(array_merge($z));  

and the result is

Array ( [0] => Array ( [0] => 8 ) [1] => Array ( [1] => 3 ) [2] => Array ( [2] => 8 ) [3] => Array ( [3] => 8 ) [4] => Array ( [4] => 8 ) [5] => Array ( [5] => 8 ) [6] => Array ( [6] => 8 ) [7] => Array ( [7] => 7 ) [8] => Array ( [8] => 7 ) [9] => Array ( [9] => 5 ) ) 1

from that result i want to try is trying to merge array from many arrays but didnt work well, if im searching on php documentation it would return like

array(
 [0] => 1
 [1] => 2
 [2] => 2
);

but i dont think what im doing here is right, and i dont know what to do next.. hope u guys can give me some advice to learn from this case.

thank you.

I found the answer from my question.. i just edit my code into like this

$count = count($data);
        for ($i = 0; $i < $count; $i++) {
            foreach($data as $row) {
                $x[$i] = $data[$i]->order_status;
            }   
        }

        $y = array_count_values($x);    
        $z = array();
        foreach($y as $key=>$value) {
            if($key == 8) {
                $text = "Finished";     

                $y[$text] = $y[$key];
                unset($y[$key]);        
            }
            elseif($key == 3) {
                $text = "Approved";

                $y[$text] = $y[$key];
                unset($y[$key]);        
            }
            elseif($key == 4) {
                $text = "Rejected";

                $y[$text] = $y[$key];
                unset($y[$key]);        
            }
            elseif($key == 7) {
                $text = "Hold";

                $y[$text] = $y[$key];
                unset($y[$key]);        
            }

        }

        echo json_encode($y);

and the result given is like i want. Thank you.

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