mysql怎么构建数组_如何从MySQL数据库构建JSON数组

小编典典

你想做这样的事情吗?

$return_arr = array();

$fetch = mysql_query("SELECT * FROM table");

while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {

$row_array['id'] = $row['id'];

$row_array['col1'] = $row['col1'];

$row_array['col2'] = $row['col2'];

array_push($return_arr,$row_array);

}

echo json_encode($return_arr);

它以以下格式返回json字符串:

[{"id":"1","col1":"col1_value","col2":"col2_value"},{"id":"2","col1":"col1_value","col2":"col2_value"}]

或类似这样的东西:

$year = date('Y');

$month = date('m');

$json_array = array(

//Each array below must be pulled from database

//1st record

array(

'id' => 111,

'title' => "Event1",

'start' => "$year-$month-10",

'url' => "http://yahoo.com/"

),

//2nd record

array(

'id' => 222,

'title' => "Event2",

'start' => "$year-$month-20",

'end' => "$year-$month-22",

'url' => "http://yahoo.com/"

)

);

echo json_encode($json_array);

2020-05-17

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值