将数组转换为 JSON 字符串 php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34942737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:17:52  来源:igfitidea点击:

Convert Array to JSON string php

phparraysjson

提问by Blessan Kurien

I have an array like this

我有一个这样的数组

$prebook=array(
             'sourceCity'=>$_POST['source'], 
             'destinationCity'=>$_POST['dest'],
             'doj'=>$_POST['doj'],
             'routeScheduleId'=>$_POST['routeid'],
              'boardingPoint'=>array(
                  'id'=>$id,
                  'location'=>$location,
                  'time'=>$time
              ),     
              'customerName'=>$_POST['fname'],
              'customerLastName'=>$_POST['lname'],
              'customerEmail'=>$_POST['email'],
              'customerPhone'=>$_POST['mobileno'],
              'emergencyPhNumber'=>$_POST['emc-number'],
              'customerAddress'=>$_POST['address'],
              'blockSeatPaxDetails'=>array(array(
                  'age'=>$_POST['age'][$key],
                  'name'=>$value,
                  'seatNbr'=>$_POST['seat-no'][$key],
                  'Sex'=>$_POST['gender'.$no],
                  'fare'=>$_POST['base-fare'][$key],
                  'totalFareWithTaxes'=>$_POST['amount'][$key],
                  'ladiesSeat'=>$ladies,
                  'lastName'=>$_POST['plname'][$key],
                  'mobile'=>$_POST['mobileno'],
                  'title'=>'Mr',
                  'email'=>$_POST['email'],
                  'idType'=>$_POST['idtype'],
                  'idNumber'=>$_POST['id-number'],
                  'nameOnId'=>$value,
                  'primary'=>true,
                  'ac'=>$ac,
                  'sleeper'=>$sleeper
         )),
                'inventoryType'=>$_POST['invtype']             
           )

From this i want to get Json string look like this

从这里我想让 Json 字符串看起来像这样

apiBlockTicketRequest:{"sourceCity":"Hyderabad","destinationCity":"Bangalore","doj":"2016-01-22","routeScheduleId":"6717","boardingPoint":{"id":"2889","location":"Mettuguda,Opp. Mettuguda Church","time":"04:50PM"},"customerName":"jj","customerLastName":"jjj","customerEmail":"[email protected]","customerPhone":"7779","emergencyPhNumber":"7878","customerAddress":"gjgj","blockSeatPaxDetails":[{"age":"22","name":"hjhj","seatNbr":"G4","Sex":"F","fare":"900","totalFareWithTaxes":"945","ladiesSeat":false,"lastName":"hjhj","mobile":"7779","title":"Mr","email":"[email protected]","idType":"Aadhar Card","idNumber":"jkjk","nameOnId":"hjhj","primary":true,"ac":false,"sleeper":false}],"inventoryType":"0"}

Here is my code

这是我的代码

$data =json_encode($prebook);
$json='apiBlockTicketRequest:'.$data;
echo $json;

But when i Validate the JSON string using thisI will get the following error

但是当我使用验证 JSON 字符串时,我会收到以下错误

Expecting object or array, not string.[Code 1, Structure 1]

Error:Strings should be wrapped in double quotes.

需要对象或数组,而不是字符串。[代码 1,结构 1]

错误:字符串应该用双引号括起来。

回答by Robert

You creating invalid json by adding apiBlockTicketRequestto output

您通过添加apiBlockTicketRequest到输出来创建无效的 json

 $json='apiBlockTicketRequest'.$data;

instead you can do

相反,你可以做

$json = json_encode(['apiBlockTicketRequest' => $prebook]);

回答by zilth

Your code is correct to make that stringbut the entire output string ($json)will never parse as json, only the $datayou encoded.

您的代码是正确的,string但整个输出字符串($json)永远不会解析为json,只有$data您编码的。