这是对我之前的问题 How can I rewrite my sql query so that it returns the values in a specific format (using mysql or php)? 的跟进.我想更改我的 sql 查询,以便以特定方式获得结果,但其中一位用户建议我保留查询原样,并相应地解析 php 中的所有内容。
所以为了让问题简短,我有一个 sql 查询:
SELECT * FROM (SELECT DATEDIFF(now(), start_time) AS days_ago, number_id,
COUNT(text_id) AS num_texts FROM TEXTS WHERE start_time BETWEEN
DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW() GROUP BY DATE(start_time),
number_id) AS temp
它以如下形式返回值:
[{"days_ago":"7","number_id":"1","num_texts":"179"},
{"days_ago":"7","number_id":"5","num_texts":"1"},
{"days_ago":"6","number_id":"1","num_texts":"61"},
{"days_ago":"6","number_id":"2","num_texts":"1"},
{"days_ago":"6","number_id":"5","num_texts":"1"},
{"days_ago":"5","number_id":"6","num_texts":"3"},
{"days_ago":"5","number_id":"3","num_texts":"1"},
{"days_ago":"4","number_id":"1","num_texts":"2"},
{"days_ago":"2","number_id":"2","num_texts":"2"},
{"days_ago":"1","number_id":"4","num_texts":"1"},
现在,我想在 php 中解析它,以便结果如下所示:
[{"days_ago": "7", "number_id" : "1", "num_texts" : "18", "number_id" : "2", "num_texts" : "12", "number_id" : "3", "num_texts" : "12" , ... , "number_id" : "6", "num_texts" : "1"},
{"days_ago": "6", "number_id" : "1", "num_texts" : "18", "number_id" : "2", "num_texts" : "12", "number_id" : "3", "num_texts" : "12" , ... , "number_id" : "6", "num_texts" : "1"},
{"days_ago": "5", "number_id" : "1", "num_texts" : "18", "number_id" : "2", "num_texts" : "12", "number_id" : "3", "num_texts" : "12" , ... , "number_id" : "6", "num_texts" : "1"},
{"days_ago": "4", "number_id" : "1", "num_texts" : "18", "number_id" : "2", "num_texts" : "12", "number_id" : "3", "num_texts" : "12" , ... , "number_id" : "6", "num_texts" : "1"},
{"days_ago": "3", "number_id" : "1", "num_texts" : "18", "number_id" : "2", "num_texts" : "12", "number_id" : "3", "num_texts" : "12" , ... , "number_id" : "6", "num_texts" : "1"},
{"days_ago": "2", "number_id" : "1", "num_texts" : "18", "number_id" : "2", "num_texts" : "12", "number_id" : "3", "num_texts" : "12" , ... , "number_id" : "6", "num_texts" : "1"},
{"days_ago": "1", "number_id" : "1", "num_texts" : "18", "number_id" : "2", "num_texts" : "12", "number_id" : "3", "num_texts" : "12" , ... , "number_id" : "6", "num_texts" : "1"}]
基本上每一行都应该有关于 days_ago 的信息和 6 个 number_id 中每一个的值 num_texts。
一位用户建议用 php 构建它:
But it will probably be a lot cleaner if you do it in php by tracking when days_ago changes and adding a new JSON record. You will also have to make sure no number_id is skipped and add a zero value for num_texts for any skipped.
Addition, sample pseudo-code:
$days_ago=0;
while ([read_records]) {
if (record['days_ago']!=$days_ago {
$days_ago!=0 [Close line]
$days_ago=record['days_ago'];
[Add new line]
[Add days_ago field] }
[Add number_id and num_texts fields]
}
[Close line]
而且我不知道如何在 php 中扩展这个伪代码..
我开始编写一些基本的 php 代码,但老实说我不知道如何根据他的建议进行:
$myArray = array();
if ($result = $mysqli->query("SELECT * FROM (SELECT DATEDIFF(now(), start_time) AS days_ago, number_id,
COUNT(text_id) AS num_texts FROM TEXTS WHERE start_time BETWEEN
DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW() GROUP BY DATE(start_time),
number_id) AS temp")) {
$days_ago = 0;
while($row = $result->fetch_array(MYSQL_ASSOC)) {
echo $row['days_ago'];
if($row['days_ago'] !=$days_ago){
}
$myArray[] = $row;
}
// echo json_encode($myArray);
}
你能帮我吗?
最佳答案
下面的代码应该生成接近您想要的 JSON:
if ($result = $mysqli->query("SELECT * FROM (SELECT DATEDIFF(now(), start_time) AS days_ago, number_id,
COUNT(text_id) AS num_texts FROM TEXTS WHERE start_time BETWEEN
DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW() GROUP BY DATE(start_time),
number_id) AS temp")) {
$days_ago = 7;
$data = array();
$num_ids = array(1,2,3,4,5,6); // in case your id's are not sequential. otherwise you can just use a for loop.
// create empty array to fill gaps for days with no activity
for($i=0; $i<=$max_days_ago) {
$data[$i] = array();
foreach($num_ids as $id) {
$data[$i]['days_ago'] = array();
$data[$i]['days_ago'][$id] = array('number_id' => $id, 'num_texts' => 0);
}
}
// Put in the data you DO have
while($row = $result->fetch_array(MYSQL_ASSOC)) {
$data[$row['days_ago']]['days_ago'][$row['number_id'] = array('number_id' => $row['number_id'], 'num_texts' => $row['num_texts']);
}
echo json_encode($data);
}
应该生成类似这样的 JSON
[{'days_ago': '0', [{'number_id': '1', 'num_texts' : '1'},{'number_id': '2', 'num_texts' : '99'},...]},
{'days_ago': '1', [{'number_id': '1', 'num_texts' : '1'},{'number_id': '2', 'num_texts' : '99'},...]},
{'days_ago': '2', [{'number_id': '1', 'num_texts' : '1'},{'number_id': '2', 'num_texts' : '99'},...]},...]
这是在记事本中输入的,未经测试 - 它可能包含一些错误和拼写错误。您的原始 JSON 示例包含重复的数组键,因此我的输出与您的原始请求略有不同。
关于php - 你能帮我根据我的 mysql 查询在 php 中构建 json 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33449528/