2016-09-07 70 views
-1

我想創建一個數組(數組名:「隊列」)使用PHP對於此ArrayList ......我有PHP代碼和下面的輸出:如何創建ARRAY?

[ 
    {"datetime":"2016-06-16 17:32:32","qname":"5","qagent":"50","qevent":"7","info1":"2","info2":"91","info3":"1"}, 
    {"datetime":"2016-06-16 17:31:01","qname":"5","qagent":"50","qevent":"10","info1":"2","info2":"1466069459.6496","info3":"1"}, 
    {"datetime":"2016-06-16 16:28:41","qname":"5","qagent":"53","qevent":"8","info1":"2","info2":"113","info3":"1"}, 
    {"datetime":"2016-06-16 16:26:48","qname":"5","qagent":"53","qevent":"10","info1":"2","info2":"1466065606.6445","info3":"1"}, 
    {"datetime":"2016-06-16 15:47:25","qname":"5","qagent":"50","qevent":"7","info1":"4","info2":"454","info3":"1"}, 
    {"datetime":"2016-06-16 15:39:51","qname":"5","qagent":"50","qevent":"10","info1":"4","info2":"1466062787.6382","info3":"3"}, 
    {"datetime":"2016-06-16 15:27:45","qname":"5","qagent":"50","qevent":"8","info1":"5","info2":"339","info3":"1"}, 
    {"datetime":"2016-06-16 15:22:06","qname":"5","qagent":"50","qevent":"10","info1":"5","info2":"1466061721.6337","info3":"4"}, 
    {"datetime":"2016-06-16 15:18:16","qname":"5","qagent":"53","qevent":"7","info1":"2","info2":"50","info3":"1"}, 
    {"datetime":"2016-06-16 15:17:26","qname":"5","qagent":"53","qevent":"10","info1":"2","info2":"1466061444.6325","info3":"1"}, 
    {"datetime":"2016-06-16 15:14:06","qname":"5","qagent":"50","qevent":"7","info1":"5","info2":"60","info3":"1"}, 
    {"datetime":"2016-06-16 15:13:06","qname":"5","qagent":"50","qevent":"10","info1":"5","info2":"1466061181.6318","info3":"4"}, 
    {"datetime":"2016-06-16 14:52:52","qname":"5","qagent":"53","qevent":"7","info1":"3","info2":"50","info3":"1"}, 
    {"datetime":"2016-06-16 14:52:02","qname":"5","qagent":"53","qevent":"10","info1":"3","info2":"1466059919.6275","info3":"3"}, 
    {"datetime":"2016-06-16 14:49:50","qname":"5","qagent":"28","qevent":"1","info1":"1","info2":"1","info3":"2"}, 
    {"datetime":"2016-06-16 14:25:44","qname":"5","qagent":"53","qevent":"7","info1":"47","info2":"162","info3":"1"}, 
    {"datetime":"2016-06-16 14:23:02","qname":"5","qagent":"53","qevent":"10","info1":"47","info2":"1466058176.6227","info3":"5"}, 
    {"datetime":"2016-06-16 14:22:51","qname":"5","qagent":"53","qevent":"0","info1":"15000","info2":"","info3":""}, 
    {"datetime":"2016-06-16 14:22:35","qname":"5","qagent":"50","qevent":"0","info1":"0","info2":"","info3":""}, 
    {"datetime":"2016-06-16 14:22:30","qname":"5","qagent":"53","qevent":"0","info1":"15000","info2":"","info3":""} 
] 

這是我的PHP代碼:

<?php 

$con=mysqli_connect("localhost","root",""); 
mysqli_select_db($con,"qstatslite")or die("error"); 

$q = mysqli_query($con,"SELECT * FROM queue_stats ORDER BY queue_stats_id DESC LIMIT 20"); 


$return_arr = array(); 

while ($row = mysqli_fetch_array($q)) { 
//$return_arr['queue_stats_id'] = $row['queue_stats_id']; 
$row_array['datetime'] = $row['datetime']; 
$row_array['qname'] = $row['qname']; 
$row_array['qagent'] = $row['qagent']; 
$row_array['qevent'] = $row['qevent']; 
$row_array['info1'] = $row['info1']; 
$row_array['info2'] = $row['info2']; 
$row_array['info3'] = $row['info3']; 

    array_push($return_arr,$row_array); 
} 

$output = json_encode(array('return_arr' => $return_arr)); 
echo json_encode($return_arr); 

?> 

我希望輸出在列表之前有一個標題「隊列」..您的幫助將不勝感激。 ty

+0

當我回答時,你的問題是不那麼詳細。你想翻譯JSON到PHP,或PHP到JSON? – NoobishPro

+0

對不起,我忘了把我的PHP代碼..我已經在上面添加它。 – Brewology

+0

這很好,但是您是否想將JSON轉換爲PHP或將PHP轉換爲JSON?我不確定我是否明白你想要什麼。 – NoobishPro

回答

2

你在這裏有一個JSON數組。 你想將它翻譯成PHP中的數組,這裏有一個特殊的功能。

http://php.net/manual/en/function.json-decode.php

用法:

$queue = json_decode($json); 

完成後,您可以使用PHP的list()功能。

http://php.net/manual/en/function.list.php

用法:

$info = array('coffee', 'brown', 'caffeine'); 
list($drink, $color, $power) = $info; 
echo "$drink is $color and $power makes it special.\n"; 

- 如果你想翻譯JSON到PHP中,你可以使用json_encode();,你似乎已經知道了。但是,您正在使用json_encode()兩次,這可能會導致問題。現在

$return_arr = array(); 

while ($row = mysqli_fetch_array($q)) { 
//$return_arr['queue_stats_id'] = $row['queue_stats_id']; 
$row_array['datetime'] = $row['datetime']; 
$row_array['qname'] = $row['qname']; 
$row_array['qagent'] = $row['qagent']; 
$row_array['qevent'] = $row['qevent']; 
$row_array['info1'] = $row['info1']; 
$row_array['info2'] = $row['info2']; 
$row_array['info3'] = $row['info3']; 

    array_push($return_arr,$row_array); 
} 

$output = json_encode(array('queue' => $return_arr)); 
echo $output; 

,如果你是這個JSON送回javascript或什麼的,它應該是這樣的:

//get values 
jsonData.queue.datetime; 
jsonData.queue.qagent; 

如果你簡單地將其再次解碼回PHP的陣列,它看起來是這樣的:

<?php 
    $queue = json_decode($output)['queue']; 
?> 
相關問題