[英]Insert multidimensional json/array into multidimensional json/array in PHP
[英]Insert multidimensional array into multidimensional json in PHP
我想用數據庫中的數據將一些數據插入 json 數據的 sub 中
這是我的代碼:
$sql = "SELECT * FROM pharma_data";
$result = mysqli_query($conn, $sql);
$data = array();
while ($row = mysqli_fetch_assoc($result)){
$row_array['id'] = $row['phar_id'];
$row_array['name'] = $row['name'];
$row_array['batch'] = $row['batch_no'];
$row_array['category'] = $row['category'];
$row_array['measurement'] = $row['measurement'];
$row_array['stock'] = $row['stock'];
$row_array['price'] = $row['price'];
$row_array['date'] = $row['date_added'];
$row_array['expiration'] = $row['expiration_date'];
$row_array['type'] = $row['medicin_type'];
$row_array['supplier'] = $row['supplier'];
array_push($data,$row_array);
}
$convert = json_encode($data);
echo $convert;
但我的代碼的 output 是:
[{"id":"8","name":"Test Product Name Pharmacy","batch":"Test Product Name Pharmacy","category":"Test Category Pharmacy","measurement":"1mg","stock":"2","price":"15","date":"February 18, 2021, 11:36 am","expiration":"2021-02-18","type":"Test Medicine Type Pharmacy","supplier":"Nicole Gonzaga"},{"id":"9","name":"Alaxan","batch":"Test user medicine Supplies","category":"Test Category Pharmacy","measurement":"1mg","stock":"66","price":"63.43","date":"February 28, 2021, 4:45 pm","expiration":"2021-02-28","type":"Test Medicine Type Pharmacy","supplier":"Nicole Gonzaga"},{"id":"10","name":"Medicol","batch":"987","category":"Test Category Pharmacy","measurement":"1mg","stock":"15","price":"123.23","date":"February 28, 2021, 5:42 pm","expiration":"2021-02-28","type":"Test Medicine Type Pharmacy","supplier":"Nicole Gonzaga"},{"id":"11","name":"test","batch":"test","category":"Test Category Pharmacy","measurement":"213","stock":"32","price":"123.78","date":"February 28, 2021, 6:43 pm","expiration":"2021-02-28","type":"Test Medicine Type Pharmacy","supplier":"Nicole Gonzaga"}]
想要這樣的 output:
{
"total": 800,
"totalNotFiltered": 800,
"rows": [
{
"id": 0,
"name": "Item 0",
"price": "$0"
},
{
"id": 1,
"name": "Item 1",
"price": "$1"
}
]
}
您需要在將$data
發送到響應之前過濾掉它,這意味着我們需要一個 function myFilter
來過濾$data
元素,我們可以使用array_map()
過濾數組中的所有元素。 total
和totalFiltered
列似乎給出了$data
中的總行數,因此是count()
。 試試下面的代碼:
function myFilter($a) {
$row['id'] = $a['id'];
$row['name'] = $a['name'];
$row['price'] = $a['price'];
return $row;
};
$filterData = array(
'total' => count($data),
'totalNotFiltered' => count($data),
'rows' => array_map("myFilter", $data)
);
$convert = json_encode($filterData);
echo $convert;
演示代碼將在這里: https://ideone.com/pZCUbG
您需要轉換返回的數據。 就像是:
$data = array();
while ($row = mysqli_fetch_assoc($result)){
$row_array['id'] = $row['phar_id'];
$row_array['name'] = $row['name'];
// $row_array['batch'] = $row['batch_no'];
// $row_array['category'] = $row['category'];
// $row_array['measurement'] = $row['measurement'];
// $row_array['stock'] = $row['stock'];
$row_array['price'] = $row['price'];
// $row_array['date'] = $row['date_added'];
// $row_array['expiration'] = $row['expiration_date'];
// $row_array['type'] = $row['medicin_type'];
// $row_array['supplier'] = $row['supplier'];
$data[] = $row_array; // Same as array_push($data,$row_array);
}
$final_data = array(
'total' => count($data),
'totalNotFiltered' => '',
'rows' => $data
);
$convert = json_encode($final_data);
echo $convert;
注釋掉在這種情況下不需要的數據(也許你需要它在其他地方使用,不確定。如果是這樣,只需取消注釋)
不確定totalNotFiltered
因為我不知道你是如何計算的。 也許您可以使用它,這為您提供了一個可以使用的想法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.