How to create a JSON object
Usually, you would do something like this:
$post_data = json_encode(array('item' => $post_data));
But, as it seems you want the output to be with "{}
", you better make sure to force json_encode()
to encode as object, by passing the JSON_FORCE_OBJECT
constant.
$post_data = json_encode(array('item' => $post_data), JSON_FORCE_OBJECT);
"{}
" brackets specify an object and "[]
" are used for arrays according to JSON specification.
How to create json object with php
PHP doesn't use json itself. Just make a regular php array and push it through the json_encode function.
$stuff = array(
array( 'label' => 'name 1', 'value' => 1 ),
array( 'label' => 'name 2', 'value' => 2 ),
array( 'label' => 'name 3', 'value' => 3 ),
);
echo json_encode( $stuff );
creating json object from sql database using php
ID in quotes
ID is in quotes because it is a string, not an integer. You can change that by changing this:
array('id'=>$row[0]
to this:
array('id'=>intval($row[0])
"Pretty Printing"
Putting it on multiple lines will only affect readability but not how the data is computed - but you can prettify it: Pretty-Printing JSON with PHP
$output = json_encode(array("feed"=>$result), JSON_PRETTY_PRINT);
echo $output;
Create JSON object using PHP
$obj = new stdClass();
$obj->label="Devices per year";
$obj->data = array(
array('1999','3.0'),
array('2000','3.9'),
//and so on...
);
echo json_encode($obj);
Creating a jSON object using php loop
Since the reference array provided is not an object/non-associative you cannot create keys in PHP then json_encode it. The reference array provided simply needs a 2 element array for each segment.
Simply create the data using a PHP non-associated array. Then json_encode the data. This should match exactly the reference array you provided.
$output = array();
foreach($segmentData->segment as $segment){
array_push($output, array($segment->segmentName, $segment->total));
}
header("Content-type: application/json");
echo json_encode($output);
creating a json object inside json object using php efficiently
If each inner object is a result-set from the SQL query then you just need single for-loop to create kind of JSON string.
Create main array
$main_array=array();
Now iterate over result from MySQL like this and append them in main array.
/* Your MySQL logic to fetch result */
$count=1;
while($row = $result->fetch_assoc()) { // assuming fetch method, you can replace it with yours.
$main_array["post".$count]=$row;
$count++;
}
$json_string=json_encode($main_array);
I think this is the best solution in which you can create the required JSON string.
How to generate JSON data with PHP?
To generate JSON in PHP, you need only one function, json_encode().
When working with database, you need to get all the rows into array first. Here is a sample code for mysqli
$sql="select * from Posts limit 20";
$result = $db->query($sql);
$posts = $result->fetch_all(MYSQLI_ASSOC);
then you can either use this array directly or make it part of another array:
echo json_encode($posts);
// or
$response = json_encode([
'posts' => $posts,
]);
if you need to save it in a file then just use file_put_contents()
file_put_contents('myfile.json', json_encode($posts));
Related Topics
Email PDF Attachment with PHP Using Fpdf
How to Only Use Created_At in Laravel
Generating a Waveform Using Ffmpeg
Comparing Bcrypt Hash Between PHP and Nodejs
Laravel Use Same Form for Create and Edit
Apple Push Notification with Sending Custom Data
PHP Warning:Mysqli_Num_Rows() Expects Parameter 1 to Be MySQLi_Result, Object Given
Is There a Function to Make a Copy of a PHP Array to Another
How to Design a Hierarchical Role Based Access Control System
Best Way to Check If a Url Is Valid
Detect "Overall Average" Color of the Picture
Using Comparison Operators in a PHP 'Switch' Statement
Minifying Final HTML Output Using Regular Expressions with Codeigniter
How to Detect and Handle MySQL Warnings with PHP
Php: Type Hinting - Difference Between 'Closure' and 'Callable'