Mycode is given below:
// seatsToCancel=U23,L43,U12
$tin='S4243534'; //Booking id
$seatsToCancel=$_GET['seatsToCancel'];
$seatArray=explode(",",$seatsToCancel);
$seatCount=count($seatArray);
$SeatsTocancel=implode ( '", "', $seatArray );
$params = array(
"tin"=>"$tin",
"seatsToCancel"=>array($SeatsTocancel)
);
echo $params=json_encode($params);
I want output like this:
{"tin":"S4243534" ,"seatsToCancel":["U23","L43","U12"]}
I assume your $seatArray
is ["U23","L43","U12"]
?
In that case you can do like this:
$seatArray = ["U23","L43","U12"];
$finalarray = ["tin"=>$tin,"seatsToCancel" => $seatArray];
echo json_encode($finalarray); //{"tin":"S4243534","seatsToCancel":["U23","L43","U12"]}
Edited to add the new requirement from edited question
So I'm guessing that your seats to cancel are coming in as a comma-separated list from the URL. So all you need to do is explode()
the GET
parameter to turn it into an array, place it as the value to a key
and json_encode()
it.
ie
$seatsToCancel = explode(',', $_GET['seatsToCancel']);
$params = [
'tin' => $tin,
'seatsToCancel' => $seatsToCancel,
];
$paramsJson = json_encode($params);
That should get you the result you're after:
{"tin":"S4243534" ,"seatsToCancel":["U23","L43","U12"]}
You may want to consider what happens if the GET
parameter is empty and whether or not you want any additional error checking.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.