How do I create JSON output in php using the defined format
Get the solution ↓↓↓Solution:
It's easy if you replace all{
with[
, and:
with=>
, then you'll have php that represents your target json
$json = [
"header" => "My header",
"pages" => [
[
"id" => 1,
"items" => [
[
"header" => "My first page",
"text" => "This is my first page",
], [
"header" => "My second page",
"text" => "This is my second page",
],
],
],
],
];
echo json_encode($json);
from that point on you can extract the and replace the parts you want with variables.
Answer
Solution:
Try this one, just create an empty php file, and paste my code, I have tested it to match yours :
<?php
$items = array();
$pages = array();
$items[] = array("header" => "My first page", "text" => "This is my first page");
$items[] = array("header" => "My second page", "text" => "This is my second page");
$pages[] = array("id" => 1, "items" => $items);
$obj = array("header" => "My header", "pages" => $pages);
echo json_encode($obj);
?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type bool in
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.