【问题标题】:Insert multidimensional array into multidimensional json in PHP在PHP中将多维数组插入多维json
【发布时间】:2021-05-30 05:27:20
【问题描述】:

我想用数据库中的数据将一些数据插入到 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;

但我的代码的输出是:

[{"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"}]

想要这样的输出:

{
  "total": 800,
  "totalNotFiltered": 800,
  "rows": [
    {
      "id": 0,
      "name": "Item 0",
      "price": "$0"
    },
    {
      "id": 1,
      "name": "Item 1",
      "price": "$1"
    }
  ]
}

enter image description here

【问题讨论】:

  • 计算 "total": 800, "totalNotFiltered": 800, 的逻辑是什么?
  • echo $convert之前添加echo "<pre>",它应该可以根据需要美化代码。

标签: php arrays mysqli


【解决方案1】:

您需要在将$data 发送到响应之前过滤掉它,这意味着我们需要一个函数myFilter 来过滤$data 元素,我们可以使用array_map() 过滤数组中的所有元素。 totaltotalFiltered 列似乎给出了 $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

【讨论】:

    【解决方案2】:

    您需要转换返回的数据。比如:

    $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,因为我不知道您是如何计算的。也许你可以使用它,这会给你一个可以使用的想法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-27
      • 2013-10-06
      • 2011-12-12
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      相关资源
      最近更新 更多