Display JSON data in php or HTML - php

{ "admin": { "FNAME": "System", "LNAME": "Administrator", "PACKAGE": "default", "WEB_TEMPLATE": "default", "BACKEND_TEMPLATE": "", "PROXY_TEMPLATE": "default", "DNS_TEMPLATE": "default", "WEB_DOMAINS": "unlimited", "WEB_ALIASES": "unlimited", "DNS_DOMAINS": "unlimited", "DNS_RECORDS": "unlimited", "MAIL_DOMAINS": "unlimited", "MAIL_ACCOUNTS": "unlimited", "DATABASES": "unlimited", "CRON_JOBS": "unlimited", "DISK_QUOTA": "unlimited", "BANDWIDTH": "unlimited", "NS": "ns1.clark-chen.com,ns2.clark-chen.com", "SHELL": "sh", "BACKUPS": "3", "CONTACT": "admin#example.com", "CRON_REPORTS": "yes", "RKEY": "moEzAxtO9j", "SUSPENDED": "no", "SUSPENDED_USERS": "0", "SUSPENDED_WEB": "0", "SUSPENDED_DNS": "0", "SUSPENDED_MAIL": "0", "SUSPENDED_DB": "0", "SUSPENDED_CRON": "0", "IP_AVAIL": "2", "IP_OWNED": "2", "U_USERS": "24", "U_DISK": "1694", "U_DISK_DIRS": "235", "U_DISK_WEB": "1255", "U_DISK_MAIL": "1", "U_DISK_DB": "203", "U_BANDWIDTH": "1932", "U_WEB_DOMAINS": "14", "U_WEB_SSL": "7", "U_WEB_ALIASES": "19", "U_DNS_DOMAINS": "1", "U_DNS_RECORDS": "11", "U_MAIL_DOMAINS": "5", "U_MAIL_DKIM": "4", "U_MAIL_ACCOUNTS": "1", "U_DATABASES": "9", "U_CRON_JOBS": "8", "U_BACKUPS": "3", "LANGUAGE": "tw", "TIME": "01:02:56", "DATE": "2015-11-15" }, "test": { "FNAME": "test", "LNAME": "test2", "PACKAGE": "default", "WEB_TEMPLATE": "default", "BACKEND_TEMPLATE": "", "PROXY_TEMPLATE": "default", "DNS_TEMPLATE": "default", "WEB_DOMAINS": "unlimited", "WEB_ALIASES": "unlimited", "DNS_DOMAINS": "unlimited", "DNS_RECORDS": "unlimited", "MAIL_DOMAINS": "unlimited", "MAIL_ACCOUNTS": "unlimited", "DATABASES": "unlimited", "CRON_JOBS": "unlimited", "DISK_QUOTA": "unlimited", "BANDWIDTH": "unlimited", "NS": "ns1.example.com,ns2.example.com", "SHELL": "nologin", "BACKUPS": "3", "CONTACT": "test#test.org", "CRON_REPORTS": "yes", "RKEY": "vsZH3vrZsJ", "SUSPENDED": "no", "SUSPENDED_USERS": "0", "SUSPENDED_WEB": "0", "SUSPENDED_DNS": "0", "SUSPENDED_MAIL": "0", "SUSPENDED_DB": "0", "SUSPENDED_CRON": "0", "IP_AVAIL": "2", "IP_OWNED": "0", "U_USERS": "0", "U_DISK": "32", "U_DISK_DIRS": "1", "U_DISK_WEB": "29", "U_DISK_MAIL": "0", "U_DISK_DB": "2", "U_BANDWIDTH": "22", "U_WEB_DOMAINS": "2", "U_WEB_SSL": "0", "U_WEB_ALIASES": "2", "U_DNS_DOMAINS": "2", "U_DNS_RECORDS": "18", "U_MAIL_DOMAINS": "0", "U_MAIL_DKIM": "0", "U_MAIL_ACCOUNTS": "0", "U_DATABASES": "2", "U_CRON_JOBS": "0", "U_BACKUPS": "3", "LANGUAGE": "en", "TIME": "10:45:07", "DATE": "2015-07-20" }}`
`I need help on display those JSON data on either php or HTML, I just need it to be easy to look.

You can do something like this:
// here $json is your json data
$arr = json_decode($json, true);
foreach($arr as $a){
foreach($a as $key => $value){
echo $key . " : " . $value . "<br />";
}
}

This would work too, if you dont mind showing the "Array" words:
$array = json_decode($json, true);
print '<pre>';
print_r($array);
print '</pre>';
Output:
Array
(
[admin] => Array
(
[FNAME] => System
[LNAME] => Administrator
...
[TIME] => 01:02:56
[DATE] => 2015-11-15
)
[test] => Array
(
[FNAME] => test
[LNAME] => test2
...
[TIME] => 10:45:07
[DATE] => 2015-07-20
)
)

Valid your JSON in http://jsonlint.com/
Or in http://jsonviewer.stack.hu/ you can validate your json and have a view in the viewer.
Also,there is a plugin named JSONViewer in chrome webstore or JSONView addon in firefox. By simply echoing the JSON and then there will be a page that you could view JSON documents in the browser.

Related

how to do pagination complex array in cakephp rest-api?

CakePHP Rest-API pagination in an array of array. Like, containable model pagination in CakePHP for reducing API load.
Some existing code :
$this->request->data['page'];
$data = $this->Model1->find('all',array(
'contain' => array('Model2'),
'fields' => array(),
'conditions' => array('Model1.status' => 1 )
));
Below API response and I want to paginate in ProductUser array because when lots of product load at one-time app will be hang. It's possible or any other way to reduce data load in CakePHP.
API Response :
"status": true,
"message": "Available devices!",
"data": [
{
"id": "6",
"name": "Television",
"parent_id": "0",
"image": "",
"status": "1",
"created": "2019-06-10 15:22:44",
"modified": "2019-06-10 15:22:44",
"ProductUser": [ //Paginate this array
{
"id": "1",
"user_id": "17",
"category_id": "6",
"brand_id": "1",
"product_id": "17",
"date_of_purchase": "2019-06-22",
"seller_id": "1",
"warranty_year": "0",
"warranty_month": "0",
"warranty_end_date": null,
"purchese_price": null,
"bill_upload": "",
"emi_year": "0",
"emi_month": "0",
"emi_start_date": null,
"emi_end_date": null,
"insurance_company_id": "0",
"insurance_year": "0",
"insurance_month": "0",
"insurance_end_date": null,
"created": "2019-06-22 14:13:01",
"modified": "2019-06-22 14:17:07",
"Product": {
"id": "17",
"category_id": "6",
"brand_id": "1",
"product_code": "Samsung LED TV 39 inch",
"name": "Samsung LED TV 39 inch",
"selling_price": "13000",
"short_description": "Television",
"image": "catalog\/sam39.jpg",
"status": "1",
"created": "2019-05-14 00:00:00",
"modified": "2019-05-14 00:00:00"
}
},
{
"id": "1",
"user_id": "17",
"category_id": "6",
"brand_id": "1",
"product_id": "17",
"date_of_purchase": "2019-06-22",
"seller_id": "1",
"warranty_year": "0",
"warranty_month": "0",
"warranty_end_date": null,
"purchese_price": null,
"bill_upload": "",
"emi_year": "0",
"emi_month": "0",
"emi_start_date": null,
"emi_end_date": null,
"insurance_company_id": "0",
"insurance_year": "0",
"insurance_month": "0",
"insurance_end_date": null,
"created": "2019-06-22 14:13:01",
"modified": "2019-06-22 14:17:07",
"Product": {
"id": "17",
"category_id": "6",
"brand_id": "1",
"product_code": "Samsung LED TV 39 inch",
"name": "Samsung LED TV 39 inch",
"selling_price": "13000",
"short_description": "Television",
"image": "catalog\/sam39.jpg",
"status": "1",
"created": "2019-05-14 00:00:00",
"modified": "2019-05-14 00:00:00"
}
}
]
}]

Parsing (3rd) Nested JSON with PHP

I am trying to parse following JSON with PHP but at the very last level ("bank") having some issues, following is the information:
JSON:
{
"loan": {
"fu": "1046",
"vb": "84",
"loan_type": "1",
"type_cocg": "14",
"meeting_place": "PLACE",
"meeting_date": "2019-05-29",
"creation_date": "2019-05-29 12:49:53",
"user_id": "1001-1556",
"member": [{
"mem_id": "1",
"name": "FIRST MEMBER",
"parentage": "PARENTAGE",
"cnic": "3393399393393",
"gender": "1",
"dob": "1994-05-29",
"marital_status": "1",
"spouse_name": "SPOUSE",
"spouse_cnic": "9939439939393",
"pres_address": "PRES ADDRESS",
"perma_address": "PERMA ADDRESS",
"mother_name": "MOTHER NAME",
"cell": "94494944949",
"loan_amount": "30000",
"network": "1",
"sim_ownership": "2",
"co_status": "3",
"occupation_category": "2",
"agri_occ": "null",
"nonagri_occ": "3",
"education": "1",
"disability": "2",
"religion": "6",
"head": "2",
"purpose": "2",
"repayment_mode": "null",
"duration": "4",
"purpose_ent": "null",
"purpose_agri": "null",
"area_unit": "2",
"agri_investment": "",
"agri_expense": "",
"purpose_livestock": "3",
"loan_id_mem": "1",
"monthly_income": "15000",
"monthly_expense": "2000",
"monthly_saving": "13000",
"yearly_saving": "156000",
"male": "2",
"female": "2",
"children": "2",
"cow": "2",
"buffalo": "2",
"goat": "2",
"sheep": "2",
"agri_area_unit": "1",
"land_own": "3",
"land_lease": "3",
"house_own": "3",
"house_rent": "3",
"caste": "CASTE",
"active_loan": "1",
"bank": [{
"id": "1",
"loan_id": "1",
"loan_mem_id": "1",
"bank_id": "1",
"bank_loan": "",
"bank_remaining": "2000",
"purpose": "1",
"purpose_agri": "16",
"purpose_livestock": "null",
"purpose_ent": "null"
}, {
"id": "2",
"loan_id": "1",
"loan_mem_id": "1",
"bank_id": "6",
"bank_loan": "",
"bank_remaining": "500",
"purpose": "3",
"purpose_agri": "16",
"purpose_livestock": "null",
"purpose_ent": "14"
}]
}, {
"mem_id": "2",
"name": "SECOND MEMBER",
"parentage": "PARENTAGE",
"cnic": "3939939393399",
"gender": "1",
"dob": "1994-05-29",
"marital_status": "1",
"spouse_name": "SPOUSE",
"spouse_cnic": "4949949494999",
"pres_address": "ADDRESS",
"perma_address": "ADDRESS",
"mother_name": "MOTHER",
"cell": "49494949494",
"loan_amount": "20000",
"network": "1",
"sim_ownership": "2",
"co_status": "2",
"occupation_category": "2",
"agri_occ": "null",
"nonagri_occ": "2",
"education": "1",
"disability": "1",
"religion": "1",
"head": "1",
"purpose": "1",
"repayment_mode": "null",
"duration": "3",
"purpose_ent": "null",
"purpose_agri": "16",
"area_unit": "1",
"agri_investment": "1500",
"agri_expense": "2000",
"purpose_livestock": "3",
"loan_id_mem": "1",
"monthly_income": "15000",
"monthly_expense": "200",
"monthly_saving": "14800",
"yearly_saving": "177600",
"male": "0",
"female": "0",
"children": "2",
"cow": "2",
"buffalo": "2",
"goat": "2",
"sheep": "2",
"agri_area_unit": "1",
"land_own": "3",
"land_lease": "3",
"house_own": "3",
"house_rent": "2",
"caste": "CASTE 2",
"active_loan": "1",
"bank": [{
"id": "3",
"loan_id": "1",
"loan_mem_id": "2",
"bank_id": "6",
"bank_loan": "",
"bank_remaining": "300",
"purpose": "1",
"purpose_agri": "43",
"purpose_livestock": "null",
"purpose_ent": "null"
}]
}]
}
}
PHP code:
$json = json_decode($content, true);
$json['loan']['fu']; // This works !
foreach($json['loan']['member'] as $item) {
$name = $item['name']; // This works !
foreach($json['loan']['member']['bank'] as $bank_item) { // THIS DOES NOT WORKS!
}
}
The last foreach loop gives out en error saying:
Notice: Undefined index: bank
Are there any clues as to what might be causing the issue, or is there some improved way of parsing the same JSON, that would be very helpful.
Your json parsing is fine. your accessing is missing index.
As the "bank" is inside an array of "member" you should access as $json['loan']['member'][0]['bank'] (the 0 is hard coded - you can switch for 1 also).
If you use for then you should do:
foreach($json['loan']['member'] as $item) {
$name = $item['name']; // This works !
foreach($item['bank'] as $bank_item) { // use $item
}
}
Use only a single foreach() and get the bank element value. If you further need to loop on bank element then you can use another foreach()
$json = json_decode($content, true);
foreach($json['loan']['member'] as $item) {
print_r($item['bank']);
foreach($item['bank'] as $bank_item) {
echo $bank_item;
}
}
DEMO: https://3v4l.org/qB8mV
You have missed that member is also multidimensional array
$json = json_decode($content, true);
/*
echo "<pre>";
print_r($json);
echo "</pre>";*/
foreach($json['loan']['member'] as $item => $value) {
$name = $value['name']; // This works !
foreach($json['loan']['member'][$item]['bank'] as $bank_item) { // THIS DOES NOT WORKS!
print_r($bank_item);
}
}
You can use array_walk_recursive() function also
$json = json_decode($content, true);
$i=0;
foreach($json['loan']['member'] as $item) {
array_walk_recursive($json['loan']['member'][$i]['bank'], function($value,$key) {
echo $key.' :'.$value ." \n";
});
$i++;
}
DEMO : https://3v4l.org/KDR6V

randomise part of an object array

I have an array of object .First I shuffle it.I want to sort array based on two key
"questions": [
{
"id_question": "35",
"id_subject": "63",
"id_question_pattern": "1",
"correct_marks": "1",
"in_correct_marks": "0",
"partial_marks": "0",
"id_question_interpretation": "1",
"id_comprehension": "0",
"is_approved": "13",
"question_image": "",
"solution_image": "",
"subject_name": "subject 1",
"id_sub_subject": "112",
"sub_subject_name": "Sub Subject 2",
"id_topic": "212",
"topic_name": "Sub subject 2 topic 1",
"id_sub_topic": "31",
"sub_topic_name": "subject 1 sub topic 2 Q3",
"id_question_source": "3",
"question_source_name": "Dakshana intimal\t",
"id_difficult_level": "4",
"difficulty_name": "Difficult",
"quesion_pattern_name": "Single Correct Option Type ",
"id_status": "0",
"status_name": "Active",
"last_review_date": "2018-10-31 11:05:14",
"id_review_requirement": "2",
"id_time_for_question": "2",
"answer": "",
"is_answered": "1",
"is_visited": "1",
"mark_for_review": "1",
"is_not_answered": "1",
"id_selected_option": "",
"single_correct_option": "",
"number_of_visted": "0",
"spend_time": "0",
"multiple_correct_option": "",
"matxi_answer": "",
"id_subject_section": "",
"sequence_number": 1
},
{
"id_question": "11",
"id_subject": "6",
"id_question_pattern": "1",
"correct_marks": "2",
"in_correct_marks": "1",
"partial_marks": "0",
"id_question_interpretation": "1",
"id_comprehension": "0",
"is_approved": "13",
"question_image": "",
"solution_image": "",
"subject_name": "Mathematics",
"id_sub_subject": "5",
"sub_subject_name": "Algebra",
"id_topic": "31",
"topic_name": "Mathematical Induction",
"id_sub_topic": "44",
"sub_topic_name": "Mathematical induction 1",
"id_question_source": "3",
"question_source_name": "Dakshana intimal\t",
"id_difficult_level": "3",
"difficulty_name": "Medium",
"quesion_pattern_name": "Single Correct Option Type ",
"id_status": "0",
"status_name": "Active",
"last_review_date": "2018-10-24 16:20:13",
"id_review_requirement": "1",
"id_time_for_question": "3",
"answer": "",
"is_answered": "1",
"is_visited": "1",
"mark_for_review": "1",
"is_not_answered": "1",
"id_selected_option": "",
"single_correct_option": "",
"number_of_visted": "0",
"spend_time": "0",
"multiple_correct_option": "",
"matxi_answer": "",
"id_subject_section": "",
"sequence_number": 2
},
{
"id_question": "25",
"id_subject": "4",
"id_question_pattern": "1",
"correct_marks": "2",
"in_correct_marks": "0",
"partial_marks": "0",
"id_question_interpretation": "2",
"id_comprehension": "0",
"is_approved": "13",
"question_image": "",
"solution_image": "",
"subject_name": "Chemistry",
"id_sub_subject": "1",
"sub_subject_name": "Optics",
"id_topic": "1",
"topic_name": "Thermo dynamics",
"id_sub_topic": "46",
"sub_topic_name": "sub topic thermodyn chemistry",
"id_question_source": "3",
"question_source_name": "Dakshana intimal\t",
"id_difficult_level": "1",
"difficulty_name": "Very Easy",
"quesion_pattern_name": "Single Correct Option Type ",
"id_status": "0",
"status_name": "Active",
"last_review_date": "2018-10-26 12:05:28",
"id_review_requirement": "1",
"id_time_for_question": "1",
"answer": "",
"is_answered": "1",
"is_visited": "1",
"mark_for_review": "1",
"is_not_answered": "1",
"id_selected_option": "",
"single_correct_option": "",
"number_of_visted": "0",
"spend_time": "0",
"multiple_correct_option": "",
"matxi_answer": "",
"id_subject_section": "",
"sequence_number": 3
},
{
"id_question": "6",
"id_subject": "4",
"id_question_pattern": "1",
"correct_marks": "2",
"in_correct_marks": "0",
"partial_marks": "0",
"id_question_interpretation": "2",
"id_comprehension": "0",
"is_approved": "13",
"question_image": "",
"solution_image": "",
"subject_name": "Chemistry",
"id_sub_subject": "1",
"sub_subject_name": "Optics",
"id_topic": "1",
"topic_name": "Thermo dynamics",
"id_sub_topic": "46",
"sub_topic_name": "sub topic thermodyn chemistry",
"id_question_source": "3",
"question_source_name": "Dakshana intimal\t",
"id_difficult_level": "2",
"difficulty_name": "Easy",
"quesion_pattern_name": "Single Correct Option Type ",
"id_status": "0",
"status_name": "Active",
"last_review_date": "2018-10-24 16:15:48",
"id_review_requirement": "1",
"id_time_for_question": "2",
"answer": "",
"is_answered": "1",
"is_visited": "1",
"mark_for_review": "1",
"is_not_answered": "1",
"id_selected_option": "",
"single_correct_option": "",
"number_of_visted": "0",
"spend_time": "0",
"multiple_correct_option": "",
"matxi_answer": "",
"id_subject_section": "",
"sequence_number": 4
}
]
I want to change id_question with in_subject every time.
foreach ($questions_array as $key => $row){
$subject_sort[$key] = $row['id_subject'];
$question_pattern_sort[$key] = $row['id_question_pattern'];
}
array_multisort($question_pattern_sort, SORT_ASC, $subject_sort, SORT_ASC, $questions_array);
If I understand your question correctly, what you can do is loop through your array and set id_question = id_subject for each element of the array.
Like so:
foreach($questions_array['questions'] as $key=>$value){
$questions_array['questions'][$key]['id_question'] = $questions_array['questions'][$key]['id_subject'];
}
To sort by id_subject then id_question_pattern, I can recommend one of two approaches (depending on your php version).
The spaceship operator with usort(): (Demo)
usort($array["questions"],
function($a, $b){
return [$a['id_subject'], $a['id_question_pattern']] <=> [$b['id_subject'], $b['id_question_pattern']];
}
);
or with array_multisort() (Demo)
array_multisort(array_column($array["questions"], "id_subject"), SORT_ASC, array_column($array["questions"], "id_question_pattern"), SORT_ASC, $array["questions"]);
Take care to realize that you are wanting to sort within the questions subarray.

how to parse the json using php? [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
{
"result": "success",
"clientid": "1",
"serviceid": null,
"pid": null,
"domain": null,
"totalresults": "2",
"startnumber": 0,
"numreturned": 2,
"products": {
"product": [
{
"id": "1",
"clientid": "1",
"orderid": "1",
"pid": "1",
"regdate": "2015-01-01",
"name": "Starter",
"translated_name": "Starter",
"groupname": "Shared Hosting",
"translated_groupname": "Shared Hosting",
"domain": "demodomain.com",
"dedicatedip": "",
"serverid": "1",
"servername": "Saturn",
"serverip": "1.2.3.4",
"serverhostname": "saturn.example.com",
"suspensionreason": "",
"firstpaymentamount": "12.95",
"recurringamount": "12.95",
"paymentmethod": "authorize",
"paymentmethodname": "Credit Card",
"billingcycle": "Monthly",
"nextduedate": "2016-11-25",
"status": "Terminated",
"username": "demodoma",
"password": "xxxxxxxx",
"subscriptionid": "",
"promoid": "0",
"overideautosuspend": "",
"overidesuspenduntil": "0000-00-00",
"ns1": "",
"ns2": "",
"assignedips": "",
"notes": "",
"diskusage": "0",
"disklimit": "0",
"bwusage": "0",
"bwlimit": "0",
"lastupdate": "0000-00-00 00:00:00",
"customfields": {
"customfield": []
},
"configoptions": {
"configoption": []
}
},
{
"id": "2",
"clientid": "1",
"orderid": "2",
"pid": "3",
"regdate": "2015-05-20",
"name": "Plus",
"translated_name": "Plus",
"groupname": "Shared Hosting",
"translated_groupname": "Shared Hosting",
"domain": "demodomain2.net",
"dedicatedip": "",
"serverid": "2",
"servername": "Pluto",
"serverip": "2.3.4.5",
"serverhostname": "pluto.example.com",
"suspensionreason": "",
"firstpaymentamount": "24.95",
"recurringamount": "24.95",
"paymentmethod": "paypal",
"paymentmethodname": "PayPal",
"billingcycle": "Monthly",
"nextduedate": "2017-01-20",
"status": "Active",
"username": "demodom2",
"password": "xxxxxxxx",
"subscriptionid": "",
"promoid": "0",
"overideautosuspend": "",
"overidesuspenduntil": "0000-00-00",
"ns1": "",
"ns2": "",
"assignedips": "",
"notes": "",
"diskusage": "0",
"disklimit": "0",
"bwusage": "0",
"bwlimit": "0",
"lastupdate": "0000-00-00 00:00:00",
"customfields": {
"customfield": []
},
"configoptions": {
"configoption": [
{
"id": "1",
"option": "Sample Config Option",
"type": "dropdown",
"value": "Selected option value"
}
]
}
}
]
}
}
How can i parse the product details from json
You can use json decode function of php to make object then fetch data from object.
$data = 'write your json response here';
$return = json_decode($data);
print_r($return);
echo $return->result;

PHP add arraylist to existing array

I am trying to encode the data in below JSON format. Not able to add the category array inside the categories. Scratching my head for long.. can anyone help me on this?
Desired Output:
{
"chart": {
"caption": "Product-wise Quarterly Revenue vs. Profit %",
"subCaption": "Harry's SuperMart - Last Year",
"xAxisname": "Quarter",
"pYAxisName": "Sales",
"sYAxisName": "Profit %",
"numberPrefix": "$",
"sNumberSuffix": "%",
"sYAxisMaxValue": "25",
"paletteColors": "#0075c2,#1aaf5d,#f2c500",
"bgColor": "#ffffff",
"borderAlpha": "20",
"showCanvasBorder": "0",
"usePlotGradientColor": "0",
"plotBorderAlpha": "10",
"legendBorderAlpha": "0",
"legendShadow": "0",
"legendBgAlpha": "0",
"valueFontColor": "#ffffff",
"showXAxisLine": "1",
"xAxisLineColor": "#999999",
"divlineColor": "#999999",
"divLineIsDashed": "1",
"showAlternateHGridColor": "0",
"subcaptionFontBold": "0",
"subcaptionFontSize": "14",
"showHoverEffect": "1"
},
"categories": [
{
"category": [
{
"label": "Q1"
},
{
"label": "Q2"
},
{
"label": "Q3"
},
{
"label": "Q4"
}
]
}
]
}
My PHP code:
$arrData = array(
"chart" => array(
"caption"=> "Product-wise Quarterly Revenue vs. Profit %",
"subCaption"=> "Harry's SuperMart - Last Year",
"xAxisname"=> "Quarter",
"pYAxisName"=> "Sales",
"sYAxisName"=> "Profit %",
"numberPrefix"=> "$",
"sNumberSuffix"=> "%",
"sYAxisMaxValue"=> "25",
"paletteColors"=> "#0075c2,#1aaf5d,#f2c500",
"bgColor"=> "#ffffff",
"borderAlpha"=> "20",
"showCanvasBorder"=> "0",
"usePlotGradientColor"=> "0",
"plotBorderAlpha"=> "10",
"legendBorderAlpha"=> "0",
"legendShadow"=> "0",
"legendBgAlpha"=> "0",
"valueFontColor"=> "#ffffff",
"showXAxisLine"=> "1",
"xAxisLineColor"=> "#999999",
"divlineColor"=> "#999999",
"divLineIsDashed"=> "1",
"showAlternateHGridColor"=> "0",
"subcaptionFontBold"=> "0",
"subcaptionFontSize"=> "14",
"showHoverEffect"=> "1"
)
);
$arrData["categories"] = array();
$arrData["category"] = array();
//array_push($arrData["categories"],array( $arrData["category"]));
//array_push($arrData["categories"],array( $arrData["category"]));
array_push($arrData["category"], array(
"label" => "Q1",
));
array_push($arrData["category"], array(
"label" => "Q2",
));
$json_string = json_encode($arrData, JSON_PRETTY_PRINT);
echo $json_string;
}
PHP output:
{
"chart": {
"caption": "Product-wise Quarterly Revenue vs. Profit %",
"subCaption": "Harry's SuperMart - Last Year",
"xAxisname": "Quarter",
"pYAxisName": "Sales",
"sYAxisName": "Profit %",
"numberPrefix": "$",
"sNumberSuffix": "%",
"sYAxisMaxValue": "25",
"paletteColors": "#0075c2,#1aaf5d,#f2c500",
"bgColor": "#ffffff",
"borderAlpha": "20",
"showCanvasBorder": "0",
"usePlotGradientColor": "0",
"plotBorderAlpha": "10",
"legendBorderAlpha": "0",
"legendShadow": "0",
"legendBgAlpha": "0",
"valueFontColor": "#ffffff",
"showXAxisLine": "1",
"xAxisLineColor": "#999999",
"divlineColor": "#999999",
"divLineIsDashed": "1",
"showAlternateHGridColor": "0",
"subcaptionFontBold": "0",
"subcaptionFontSize": "14",
"showHoverEffect": "1"
},
"categories": [
],
"category": [
{
"label": "Q1"
},
{
"label": "Q2"
}
]
}
for ($i = 1; $i <= 4; $i++) {
$arrData['categories'][0]['category'] []= ["label" => "Q{$i}"];
}
By the way, you should use []= instead of array_push, because in that way there is no overhead of calling a function.

Categories