Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Array to JSON string php

Tags:

json

arrays

php

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

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 this I will get the following error

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

Error:Strings should be wrapped in double quotes.

like image 560
Blessan Kurien Avatar asked Jan 22 '16 09:01

Blessan Kurien


People also ask

What is json_encode function in PHP?

The json_encode() function can return a string containing the JSON representation of supplied value. The encoding is affected by supplied options, and additionally, the encoding of float values depends on the value of serialize_precision.

Which function in PHP can be used to convert objects in PHP into JSON?

json_encode() is a native PHP function that allows you to convert PHP data into the JSON format. The function takes in a PHP object ($value) and returns a JSON string (or False if the operation fails).

How do you convert associative array to string?

To convert an array to a string, one of the common ways to do that is to use the json_encode() function which is used to returns the JSON representation of a value. This function takes any value as input except resource.

How can I get JSON encoded data in PHP?

To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.


1 Answers

You creating invalid json by adding apiBlockTicketRequest to output

 $json='apiBlockTicketRequest'.$data;

instead you can do

$json = json_encode(['apiBlockTicketRequest' => $prebook]);
like image 72
Robert Avatar answered Oct 05 '22 07:10

Robert