How to create a json like the following using php?
I am writing an API for Angular application, using PHP Codeigniter.
I am Unable to create a json response for my application.
Please find the reference URL https://ionicacademy.com/accordion-list-ionic/
I am trying to create multi-level accordion using the above tuts.
This is what I am getting from my code
{
"items":[
{
"name": "VAL 1",
"children": [
{
"name": "Topic 1"
},
{
"name": {
"children": "Children 2"
}
},
{
"name": "Topic 3"
},
{
"name": {
"children": "Children 2"
}
}
]
}
]
}
Find the below code for reference.
$sec_pair = array('1' => 'VAL 1', '2' => 'VAL 2', '3' => 'VAL 3');
$data['items'] = array();
if(!empty($videos)) {
foreach ($videos AS $key => $item) {
$title_str = !empty($sec_pair[$key]) ? $sec_pair[$key] : '';
$topics = array();
$topics['name'] = $title_str;
foreach ($item AS $k => $i_val) {
$lectures_title = $this->my_model->select_db('topics', 'id,title', array('id' => $k));
$topic_title = !empty($lectures_title[0]->title) ? $lectures_title[0]->title : '';
$topics['children']['name'] = $topic_title;
$topics['children']['name']['children'] = 'Children 2';
}
$data['items'] = $topics;
}
}
header('Content-Type: application/json');
echo json_encode($data, TRUE);
exit;
This is what I want
{
"items": [
{
"name": "Action",
"children": [
{
"name": "Special Academy Pizza",
"information": "Pastrami pork belly ball tip andouille corned beef jerky shankle landjaeger. Chicken chuck porchetta picanha, ham brisket tenderloin venison meatloaf landjaeger jowl.",
"price": "$25"
},
{
"name": "Pizza Ionic",
"information": "Pork chop meatloaf rump, meatball shoulder turducken alcatra doner sausage capicola pork strip steak turkey cupim leberkas.",
"price": "$19.99"
}
]
},
{
"name": "Pizza",
"children": [
{
"name": "Traditional",
"children": [
{
"name": "Pizza Salami",
"information": "Pork chop jowl capicola porchetta, kielbasa prosciutto boudin bacon pork pig.",
"price": "$10"
},
{
"name": "Pizza Prosciutto",
"information": "Pork chop pastrami landjaeger chuck brisket",
"price": "$12"
}
]
},
{
"name": "Gourmet",
"children": [
{
"name": "Pizza Bombay",
"information": "Pastrami ham hock ball tip, tongue ribeye chuck ham beef bresaola leberkas.",
"price": "$13"
},
{
"name": "Pizza Crazy Dog",
"information": "Andouille spare ribs meatloaf swine ground round pork loin, brisket chuck bacon tongue.",
"price": "$14"
},
{
"name": "Pizza Italia",
"information": "Ribeye ham t-bone, tail ground round biltong picanha sausage rump corned beef.",
"price": "$11"
},
{
"name": "Pizza Tuna",
"information": "Pork chop pastrami landjaeger chuck brisket",
"price": "$14"
}
]
},
{
"name": "Bestseller",
"children": [
{
"name": "Pizza Academy",
"information": "Frankfurter tail capicola cupim shankle salami, beef ribs beef boudin porchetta ball tip leberkas turkey tenderloin.",
"price": "$25"
},
{
"name": "Pizza Ionic",
"information": "Shank chuck tail, kevin shankle ham hock pork loin pork hamburger beef ribs.",
"price": "$19.99"
}
]
}
]
},
{
"name": "Drinks",
"children": [
{
"name": "Special Academy Pizza",
"information": " Landjaeger fatback shank frankfurter, tongue shoulder ham strip steak pancetta pork short loin corned beef short ribs biltong cow",
"price": "$25"
},
{
"name": "Pizza Ionic",
"information": "Pork chop pastrami landjaeger chuck brisket",
"price": "$19.99"
}
]
}
]
}
php arrays json rest
add a comment |
I am writing an API for Angular application, using PHP Codeigniter.
I am Unable to create a json response for my application.
Please find the reference URL https://ionicacademy.com/accordion-list-ionic/
I am trying to create multi-level accordion using the above tuts.
This is what I am getting from my code
{
"items":[
{
"name": "VAL 1",
"children": [
{
"name": "Topic 1"
},
{
"name": {
"children": "Children 2"
}
},
{
"name": "Topic 3"
},
{
"name": {
"children": "Children 2"
}
}
]
}
]
}
Find the below code for reference.
$sec_pair = array('1' => 'VAL 1', '2' => 'VAL 2', '3' => 'VAL 3');
$data['items'] = array();
if(!empty($videos)) {
foreach ($videos AS $key => $item) {
$title_str = !empty($sec_pair[$key]) ? $sec_pair[$key] : '';
$topics = array();
$topics['name'] = $title_str;
foreach ($item AS $k => $i_val) {
$lectures_title = $this->my_model->select_db('topics', 'id,title', array('id' => $k));
$topic_title = !empty($lectures_title[0]->title) ? $lectures_title[0]->title : '';
$topics['children']['name'] = $topic_title;
$topics['children']['name']['children'] = 'Children 2';
}
$data['items'] = $topics;
}
}
header('Content-Type: application/json');
echo json_encode($data, TRUE);
exit;
This is what I want
{
"items": [
{
"name": "Action",
"children": [
{
"name": "Special Academy Pizza",
"information": "Pastrami pork belly ball tip andouille corned beef jerky shankle landjaeger. Chicken chuck porchetta picanha, ham brisket tenderloin venison meatloaf landjaeger jowl.",
"price": "$25"
},
{
"name": "Pizza Ionic",
"information": "Pork chop meatloaf rump, meatball shoulder turducken alcatra doner sausage capicola pork strip steak turkey cupim leberkas.",
"price": "$19.99"
}
]
},
{
"name": "Pizza",
"children": [
{
"name": "Traditional",
"children": [
{
"name": "Pizza Salami",
"information": "Pork chop jowl capicola porchetta, kielbasa prosciutto boudin bacon pork pig.",
"price": "$10"
},
{
"name": "Pizza Prosciutto",
"information": "Pork chop pastrami landjaeger chuck brisket",
"price": "$12"
}
]
},
{
"name": "Gourmet",
"children": [
{
"name": "Pizza Bombay",
"information": "Pastrami ham hock ball tip, tongue ribeye chuck ham beef bresaola leberkas.",
"price": "$13"
},
{
"name": "Pizza Crazy Dog",
"information": "Andouille spare ribs meatloaf swine ground round pork loin, brisket chuck bacon tongue.",
"price": "$14"
},
{
"name": "Pizza Italia",
"information": "Ribeye ham t-bone, tail ground round biltong picanha sausage rump corned beef.",
"price": "$11"
},
{
"name": "Pizza Tuna",
"information": "Pork chop pastrami landjaeger chuck brisket",
"price": "$14"
}
]
},
{
"name": "Bestseller",
"children": [
{
"name": "Pizza Academy",
"information": "Frankfurter tail capicola cupim shankle salami, beef ribs beef boudin porchetta ball tip leberkas turkey tenderloin.",
"price": "$25"
},
{
"name": "Pizza Ionic",
"information": "Shank chuck tail, kevin shankle ham hock pork loin pork hamburger beef ribs.",
"price": "$19.99"
}
]
}
]
},
{
"name": "Drinks",
"children": [
{
"name": "Special Academy Pizza",
"information": " Landjaeger fatback shank frankfurter, tongue shoulder ham strip steak pancetta pork short loin corned beef short ribs biltong cow",
"price": "$25"
},
{
"name": "Pizza Ionic",
"information": "Pork chop pastrami landjaeger chuck brisket",
"price": "$19.99"
}
]
}
]
}
php arrays json rest
3
Where is the error?
– Sfili_81
Jan 2 at 8:41
add a comment |
I am writing an API for Angular application, using PHP Codeigniter.
I am Unable to create a json response for my application.
Please find the reference URL https://ionicacademy.com/accordion-list-ionic/
I am trying to create multi-level accordion using the above tuts.
This is what I am getting from my code
{
"items":[
{
"name": "VAL 1",
"children": [
{
"name": "Topic 1"
},
{
"name": {
"children": "Children 2"
}
},
{
"name": "Topic 3"
},
{
"name": {
"children": "Children 2"
}
}
]
}
]
}
Find the below code for reference.
$sec_pair = array('1' => 'VAL 1', '2' => 'VAL 2', '3' => 'VAL 3');
$data['items'] = array();
if(!empty($videos)) {
foreach ($videos AS $key => $item) {
$title_str = !empty($sec_pair[$key]) ? $sec_pair[$key] : '';
$topics = array();
$topics['name'] = $title_str;
foreach ($item AS $k => $i_val) {
$lectures_title = $this->my_model->select_db('topics', 'id,title', array('id' => $k));
$topic_title = !empty($lectures_title[0]->title) ? $lectures_title[0]->title : '';
$topics['children']['name'] = $topic_title;
$topics['children']['name']['children'] = 'Children 2';
}
$data['items'] = $topics;
}
}
header('Content-Type: application/json');
echo json_encode($data, TRUE);
exit;
This is what I want
{
"items": [
{
"name": "Action",
"children": [
{
"name": "Special Academy Pizza",
"information": "Pastrami pork belly ball tip andouille corned beef jerky shankle landjaeger. Chicken chuck porchetta picanha, ham brisket tenderloin venison meatloaf landjaeger jowl.",
"price": "$25"
},
{
"name": "Pizza Ionic",
"information": "Pork chop meatloaf rump, meatball shoulder turducken alcatra doner sausage capicola pork strip steak turkey cupim leberkas.",
"price": "$19.99"
}
]
},
{
"name": "Pizza",
"children": [
{
"name": "Traditional",
"children": [
{
"name": "Pizza Salami",
"information": "Pork chop jowl capicola porchetta, kielbasa prosciutto boudin bacon pork pig.",
"price": "$10"
},
{
"name": "Pizza Prosciutto",
"information": "Pork chop pastrami landjaeger chuck brisket",
"price": "$12"
}
]
},
{
"name": "Gourmet",
"children": [
{
"name": "Pizza Bombay",
"information": "Pastrami ham hock ball tip, tongue ribeye chuck ham beef bresaola leberkas.",
"price": "$13"
},
{
"name": "Pizza Crazy Dog",
"information": "Andouille spare ribs meatloaf swine ground round pork loin, brisket chuck bacon tongue.",
"price": "$14"
},
{
"name": "Pizza Italia",
"information": "Ribeye ham t-bone, tail ground round biltong picanha sausage rump corned beef.",
"price": "$11"
},
{
"name": "Pizza Tuna",
"information": "Pork chop pastrami landjaeger chuck brisket",
"price": "$14"
}
]
},
{
"name": "Bestseller",
"children": [
{
"name": "Pizza Academy",
"information": "Frankfurter tail capicola cupim shankle salami, beef ribs beef boudin porchetta ball tip leberkas turkey tenderloin.",
"price": "$25"
},
{
"name": "Pizza Ionic",
"information": "Shank chuck tail, kevin shankle ham hock pork loin pork hamburger beef ribs.",
"price": "$19.99"
}
]
}
]
},
{
"name": "Drinks",
"children": [
{
"name": "Special Academy Pizza",
"information": " Landjaeger fatback shank frankfurter, tongue shoulder ham strip steak pancetta pork short loin corned beef short ribs biltong cow",
"price": "$25"
},
{
"name": "Pizza Ionic",
"information": "Pork chop pastrami landjaeger chuck brisket",
"price": "$19.99"
}
]
}
]
}
php arrays json rest
I am writing an API for Angular application, using PHP Codeigniter.
I am Unable to create a json response for my application.
Please find the reference URL https://ionicacademy.com/accordion-list-ionic/
I am trying to create multi-level accordion using the above tuts.
This is what I am getting from my code
{
"items":[
{
"name": "VAL 1",
"children": [
{
"name": "Topic 1"
},
{
"name": {
"children": "Children 2"
}
},
{
"name": "Topic 3"
},
{
"name": {
"children": "Children 2"
}
}
]
}
]
}
Find the below code for reference.
$sec_pair = array('1' => 'VAL 1', '2' => 'VAL 2', '3' => 'VAL 3');
$data['items'] = array();
if(!empty($videos)) {
foreach ($videos AS $key => $item) {
$title_str = !empty($sec_pair[$key]) ? $sec_pair[$key] : '';
$topics = array();
$topics['name'] = $title_str;
foreach ($item AS $k => $i_val) {
$lectures_title = $this->my_model->select_db('topics', 'id,title', array('id' => $k));
$topic_title = !empty($lectures_title[0]->title) ? $lectures_title[0]->title : '';
$topics['children']['name'] = $topic_title;
$topics['children']['name']['children'] = 'Children 2';
}
$data['items'] = $topics;
}
}
header('Content-Type: application/json');
echo json_encode($data, TRUE);
exit;
This is what I want
{
"items": [
{
"name": "Action",
"children": [
{
"name": "Special Academy Pizza",
"information": "Pastrami pork belly ball tip andouille corned beef jerky shankle landjaeger. Chicken chuck porchetta picanha, ham brisket tenderloin venison meatloaf landjaeger jowl.",
"price": "$25"
},
{
"name": "Pizza Ionic",
"information": "Pork chop meatloaf rump, meatball shoulder turducken alcatra doner sausage capicola pork strip steak turkey cupim leberkas.",
"price": "$19.99"
}
]
},
{
"name": "Pizza",
"children": [
{
"name": "Traditional",
"children": [
{
"name": "Pizza Salami",
"information": "Pork chop jowl capicola porchetta, kielbasa prosciutto boudin bacon pork pig.",
"price": "$10"
},
{
"name": "Pizza Prosciutto",
"information": "Pork chop pastrami landjaeger chuck brisket",
"price": "$12"
}
]
},
{
"name": "Gourmet",
"children": [
{
"name": "Pizza Bombay",
"information": "Pastrami ham hock ball tip, tongue ribeye chuck ham beef bresaola leberkas.",
"price": "$13"
},
{
"name": "Pizza Crazy Dog",
"information": "Andouille spare ribs meatloaf swine ground round pork loin, brisket chuck bacon tongue.",
"price": "$14"
},
{
"name": "Pizza Italia",
"information": "Ribeye ham t-bone, tail ground round biltong picanha sausage rump corned beef.",
"price": "$11"
},
{
"name": "Pizza Tuna",
"information": "Pork chop pastrami landjaeger chuck brisket",
"price": "$14"
}
]
},
{
"name": "Bestseller",
"children": [
{
"name": "Pizza Academy",
"information": "Frankfurter tail capicola cupim shankle salami, beef ribs beef boudin porchetta ball tip leberkas turkey tenderloin.",
"price": "$25"
},
{
"name": "Pizza Ionic",
"information": "Shank chuck tail, kevin shankle ham hock pork loin pork hamburger beef ribs.",
"price": "$19.99"
}
]
}
]
},
{
"name": "Drinks",
"children": [
{
"name": "Special Academy Pizza",
"information": " Landjaeger fatback shank frankfurter, tongue shoulder ham strip steak pancetta pork short loin corned beef short ribs biltong cow",
"price": "$25"
},
{
"name": "Pizza Ionic",
"information": "Pork chop pastrami landjaeger chuck brisket",
"price": "$19.99"
}
]
}
]
}
php arrays json rest
php arrays json rest
edited Jan 2 at 8:38
Khan Sharukh
asked Jan 2 at 8:27


Khan SharukhKhan Sharukh
72110
72110
3
Where is the error?
– Sfili_81
Jan 2 at 8:41
add a comment |
3
Where is the error?
– Sfili_81
Jan 2 at 8:41
3
3
Where is the error?
– Sfili_81
Jan 2 at 8:41
Where is the error?
– Sfili_81
Jan 2 at 8:41
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003261%2fhow-to-create-a-json-like-the-following-using-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003261%2fhow-to-create-a-json-like-the-following-using-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Where is the error?
– Sfili_81
Jan 2 at 8:41