如何使用PHP按键值对数组数据进行排序?

时间:2016-05-21 13:32:49

标签: php arrays sorting

我需要根据某个键值对包含json数据的数组进行排序。我在下面提供我的数组。

$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"},
]

我有上面给出的数据数组。我需要按照member_id对此进行排序,并计算hit_typetotal hit,最后需要保存到另一个数组中。我期望的输出数组如下所示。

$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"}
]

以上是我的预期output.hit_type将始终按member_id计算,表格如下所示。

name      type
Page       1
Map        2
Gallery    3
Phone      4
Web        5

编辑:评论中的查询:

$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;
"; 

1 个答案:

答案 0 :(得分:1)

你可以在查询

中完成
var crudUser = require('../crud/crudUser.js');

app.post('/signup', crudUser.createUser);