I need to sort an array which contains json data as per some key value. I am providing my array below.
$data=[
{device_type:"1",hit_type:"3",member_id:"96",name:"Gallery",rest_name:"Goro + Gun",summery_id:"22"},
{device_type:"1",hit_type:"1",member_id:"96",name:"Page",rest_name:"Goro + Gun",summery_id:"22"},
{device_type:"1",hit_type:"2",member_id:"96",name:"Map",rest_name:"Goro + Gun",summery_id:"22"},
{device_type:"1",hit_type:"2",member_id:"96",name:"Map",rest_name:"Goro + Gun",summery_id:"22"},
{device_type:"1",hit_type:"4",member_id:"90",name:"Phone",rest_name:"the livingroom",summery_id:"21"},
]
I have the array of data which is given above. I need to sort this as per member_id and counts the hit_type and total hit finally it need to save into another array. My expected output array is given below.
$result=[
{device_type:"1",member_id:"96",Page_hit:"1",Gallery_hit:"1",Map_hit:"2",Phone_hit:"0",Web_hit:"0",rest_name:"Goro + Gun",summery_id:"22",total_hit:"4"},
{device_type:"1",member_id:"90",Page_hit:"0",Gallery_hit:"0",Map_hit:"0",Phone_hit:"1",Web_hit:"0",rest_name:"the livingroom",summery_id:"21",total_hit:"1"}
]
The above is my expected output.hit_type will always count as per member_id and the table is given below.
name      type
Page       1
Map        2
Gallery    3
Phone      4
Web        5
Edit: The query from the comments:
$sql = "
SELECT s.summery_id,
       s.member_id,
       s.hit_type,
       s.device_type,
       s.counter,
       s.date,
       r.rest_name,
       h.NAME,
       h.type
FROM   db_analytics_summery AS s
       LEFT JOIN db_hit_type AS h
              ON s.hit_type = h.type
       LEFT JOIN db_restaurant_basic AS r
              ON s.member_id = r.member_id
ORDER  BY s.summery_id DESC;
"; 
 
     
    