2015-10-19 36 views
1

我有我的mysqli查询:如何只显示json中的值而不是键:值对?

$fetchTransactions = "SELECT * FROM transaction WHERE client_id = '$client_id'"; 
$result = $mysqli->query($fetchTransactions); 
$data = array();  
$data = $result->fetch_all(MYSQLI_ASSOC); 
echo json_encode($data); 

它返回JSON作为:

[{"transaction_id":"1","client_id":"2","total_price":"100.70","creation_date":"2015-10-18 03:00:00","unique_hash":"ABCDEF"}, 
{"transaction_id":"2","client_id":"2","total_price":"88.20","creation_date":"2015-10-18 04:00:00","unique_hash":"GHIJK"}] 

取而代之的是,我想在这里呈现在表单中的数据:

{ 
    "data": [ 
    [ 
     "1", 
     "2", 
     "100.70", 
     "2015-10-18 03:00:00", 
     "ABCDEF" 
    ], 
    [ 
     "2", 
     "2", 
     "88.20", 
     "2015-10-18 03:00:00", 
     "GHIJK" 
    ], 

,因为我需要这个json格式来提供从这里取得的数据表:https://datatables.net/examples/data_sources/ajax.html

你能帮我吗?

回答

1

而不是将$data转换为json,循环它。 array_values函数将从关联数组中提取所有数组值。

<?php 
$newarray = array(); 
foreach($data as $d){ 
    // save array values to $newarray 
    $newarray['data'][] = array_values($d); 
} 

$json_newarray = json_encode($newarray); 

echo print_r($json_newarray, true); 

?> 
0

使用array_map通过行值迭代和array_values忽略键(列名),你可以做到以下几点:

echo json_encode(array('data' => array_map('array_values', $data)));