-1

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;
"; 
halfer
  • 19,824
  • 17
  • 99
  • 186
satya
  • 3,508
  • 11
  • 50
  • 130
  • is it from DataBase? – splash58 May 21 '16 at 13:35
  • Yes,I can fetch the 1st array from DB. – satya May 21 '16 at 13:37
  • I started reading this and thought it was a duplicate of the FAQ http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value but reading data from a database and then sorting it is exceptionally silly. Use the database for sorting. – symcbean May 21 '16 at 22:22

1 Answers1

1

you can do it in query

SELECT s.device_type,
   s.member_id,
   sum(if(`hit_type`=1,1,0)) Page_hit,
   sum(if(`hit_type`=3,1,0)) Gallery_hit,
   ...
   r.rest_name,
   count(1) total_hit
 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_idgroup by
 group by member_id
 ORDER  BY s.summery_id DESC;
splash58
  • 26,043
  • 3
  • 22
  • 34
  • I have this query `$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"; $sql.=" from db_analytics_summery as s"; $sql.=" left join db_hit_type as h on s.hit_type=h.type"; $sql.=" left join db_restaurant_basic as r on s.member_id=r.member_id"; $sql.=" order by s.summery_id desc"; $res=mysqli_query($connect,$sql);`. – satya May 21 '16 at 13:42
  • Can you edit here so that i can get my expected out put as explained above. – satya May 21 '16 at 13:43
  • @spalsh,Is this two line(`sum(if(`hit_type`=1,1,0)) Page_hit, sum(if(`hit_type`=3,1,0)) Gallery_hit,`) are right ? – satya May 23 '16 at 05:37
  • i tried your answer but its not coming as per my requirement.Each time count of hit type is showing 0. – satya May 23 '16 at 05:48
  • Glad that could help. Good luck! – splash58 May 23 '16 at 07:17
  • Thanks again splash. – satya May 23 '16 at 07:59