php json转换成byte数组,Php json_encode array and object array

I need to create a json with this format:

{

"reservs": [

{

"ResId": "58",

"time": "2020-05-15 19:41:50",

"boxEntering": null,

"boxClosing": null,

"active": "1",

"UserId": "29",

"BoxId": "4",

"boxPlace": null,

"box": {

"id": "4",

"Nom": "Hortillonages",

"Lat": "49.8953",

"Lng": "2.31034",

"place": "0",

"placeMax": "9"

}

}

]

}

in entries, a $header who check the user token(not use for my problem)

$table the table returned from PDO::FETCHASSOC from sql SELECT request

My php code:

function generateJson($table, headerChecker $header){

$final = array();

foreach ($table as $item) {

$box = array(

"id" => $item["id"],

"Nom" => $item["Nom"],

"Lat" => $item["Lat"],

"Lng" => $item["Lng"],

"place" => $item["place"],

"placeMax" => $item["placeMax"]

);

$reserv = array(

"ResId" => $item["ResId"],

"time" => $item["time"],

"boxEntering" => $item["boxEntering"],

"boxClosing" => $item["boxClosing"],

"active" => $item["active"],

"UserId" => $item["UserId"],

"BoxId" => $item["BoxId"],

"boxPlace" => $item["boxPlace"],

);

$reserv["box"] = $box;

array_merge($final,$reserv);

}

$arr = array("reservs" => $table);

$header->tokenJson($arr);

echo json_encode($arr);

}

I have this result

{"reservs": [

{

"ResId": "58",

"time": "2020-05-15 19:41:50",

"boxEntering": null,

"boxClosing": null,

"active": "1",

"UserId": "29",

"BoxId": "4",

"boxPlace": null,

"id": "4",

"Nom": "Hortillonages",

"Lat": "49.8953",

"Lng": "2.31034",

"place": "0",

"placeMax": "9",

"QRID": "",

"boxToken": ""

}]

}

I think the Json format eror is in the array_merge function.

What add array function can I use to not remove the Box object

# Answer 1

4d350fd91e33782268f371d7edaa8a76.png

Try this one

function generateJson($table, headerChecker $header){

$final = array();

foreach ($table as $item) {

$box = array(

"id" => $item["id"],

"Nom" => $item["Nom"],

"Lat" => $item["Lat"],

"Lng" => $item["Lng"],

"place" => $item["place"],

"placeMax" => $item["placeMax"]

);

$reserv = array(

"ResId" => $item["ResId"],

"time" => $item["time"],

"boxEntering" => $item["boxEntering"],

"boxClosing" => $item["boxClosing"],

"active" => $item["active"],

"UserId" => $item["UserId"],

"BoxId" => $item["BoxId"],

"boxPlace" => $item["boxPlace"],

);

$reserv["box"] = $box;

$final[] = $reserv;

}

$arr = array("reservs" => $final);

$header->tokenJson($arr);

echo json_encode($arr);

}

# Answer 2

Your problem is that you are not assigning the return of array_merge and using the wrong variable $table. Just dynamically append to $final:

foreach ($table as $item) {

// this all appears good

//

$reserv["box"] = $box;

$final[] = $reserv; // append to $final

}

$arr = array("reservs" => $final); // use $final

$header->tokenJson($arr);

echo json_encode($arr);

Merge the new $reserv into $final and assign it to $final, then use $final.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值