How to add headers while converting JSON to CSV in PHP? - php

I have generated nested json from API call. Now I am flattened that json to csv but header is missing. I want header also in csv but not all header with data. Could you please help me to solve this problem. I have given the code and example below-
Example-
JSON-
Array
(
[id] => 1077079
[doc_roles__sysr] => Array
(
[responseDetails] => Array
(
[pagesize] => 250
[pageoffset] => 0
[size] => 3
[total] => 3
)
[data] => Array
(
[0] => Array
(
[role_name__sys] => approved_email_content_admin__c
[user__sys] =>
[group__sys] => 1461227826614
[document_id] => 1077079
)
[1] => Array
(
[role_name__sys] => owner__c
[user__sys] => 5486129
[group__sys] =>
[document_id] => 1077079
)
[2] => Array
(
[role_name__sys] => global_integration_user__c
[user__sys] =>
[group__sys] => 1461227835650
[document_id] => 1077079
)
)
)
)
It is generating a csv file like this-
1000038,250,0,26,26,approved_content_viewer_read_only_acce__c,,1461227890702,1000038
1000069,250,0,20,20,emea_skm_reviewer__c,,1461227853396,1000069
1000114,250,0,3,3,global_integration_user__c,,1461227835718,1000114
1000118,250,0,3,3,global_integration_user__c,,1461227835718,1000118
1000392,250,0,18,18,consumer__c,,1461227890914,1000392
In Output I want CSV File like this-
id,role_name__sys,user__sys,group__sys,document_id
1077079,approved_email_content_admin__c,,1461227826614,1077079
1077079,owner__c,5486129,,1077079
1077079,global_integration_user__c,,1461227835650,1077079
The code which I am using -
function array_flatten ($nonFlat) {
$flat = array();
foreach (new RecursiveIteratorIterator(
new RecursiveArrayIterator($nonFlat)) as $k=>$v) {
$flat[$k] = $v;
}
return $flat;
}
$content = sendCurlRequest($p);
//echo $content;
$content = json_decode(json_encode($content), true);
$fp = fopen('doc_roles__sys.csv', 'w');
foreach ($content['data'] as $fields) {
print_r($fields);
fputcsv($fp, array_flatten($fields));
}
fclose($fp);
var_export($content)
{"id":1051524,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":20,"total":20},"data":[{"role_name__sys":"owner__c","user__sys":8401011,"group__sys":null,"document_id":1051524},{"role_name__sys":"emea_project_owner__c","user__sys":8401011,"group__sys":null,"document_id":1051524},{"role_name__sys":"emea_submitter__c","user__sys":8401011,"group__sys":null,"document_id":1051524},{"role_name__sys":"distribution_contacts__c","user__sys":8401011,"group__sys":null,"document_id":1051524},{"role_name__sys":"business_admin_us__c","user__sys":null,"group__sys":1461227848992,"document_id":1051524},{"role_name__sys":"global_inbound_integration_user__c","user__sys":null,"group__sys":1461227874501,"document_id":1051524},{"role_name__sys":"approved_content_viewer__c","user__sys":null,"group__sys":1461227820801,"document_id":1051524},{"role_name__sys":"emea_signatories__c","user__sys":null,"group__sys":1461227847859,"document_id":1051524},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835662,"document_id":1051524},{"role_name__sys":"emea_scientific_department_3__c","user__sys":null,"group__sys":1461227848106,"document_id":1051524},{"role_name__sys":"emea_scientific_department_1__c","user__sys":null,"group__sys":1461227847862,"document_id":1051524},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227829501,"document_id":1051524},{"role_name__sys":"emea_medical_reviewer__c","user__sys":null,"group__sys":1461227848119,"document_id":1051524},{"role_name__sys":"dam_librarian__c","user__sys":null,"group__sys":1461227871686,"document_id":1051524},{"role_name__sys":"approved_content_viewer__c","user__sys":null,"group__sys":1461227830527,"document_id":1051524},{"role_name__sys":"approved_content_with_annotation_viewer__c","user__sys":null,"group__sys":1461227876601,"document_id":1051524},{"role_name__sys":"emea_skm_reviewer__c","user__sys":null,"group__sys":1461227853617,"document_id":1051524},{"role_name__sys":"approved_content_with_annotation_viewer__c","user__sys":null,"group__sys":1461227857404,"document_id":1051524},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227875206,"document_id":1051524},{"role_name__sys":"emea_scientific_department_2__c","user__sys":null,"group__sys":1461227847877,"document_id":1051524}]}},{"id":1051619,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":23,"total":23},"data":[{"role_name__sys":"owner__c","user__sys":3596955,"group__sys":null,"document_id":1051619},{"role_name__sys":"emea_project_owner__c","user__sys":3596955,"group__sys":null,"document_id":1051619},{"role_name__sys":"emea_submitter__c","user__sys":3596955,"group__sys":null,"document_id":1051619},{"role_name__sys":"emea_other_reviewer__c","user__sys":2285381,"group__sys":null,"document_id":1051619},{"role_name__sys":"emea_project_owner__c","user__sys":9395329,"group__sys":null,"document_id":1051619},{"role_name__sys":"distribution_contacts__c","user__sys":9395329,"group__sys":null,"document_id":1051619},{"role_name__sys":"emea_medical_reviewer__c","user__sys":null,"group__sys":1461227847879,"document_id":1051619},{"role_name__sys":"emea_signatories__c","user__sys":null,"group__sys":1461227847872,"document_id":1051619},{"role_name__sys":"emea_scientific_department_1__c","user__sys":null,"group__sys":1461227848105,"document_id":1051619},{"role_name__sys":"emea_scientific_department_2__c","user__sys":null,"group__sys":1461227847867,"document_id":1051619},{"role_name__sys":"global_inbound_integration_user__c","user__sys":null,"group__sys":1461227874501,"document_id":1051619},{"role_name__sys":"approved_content_viewer__c","user__sys":null,"group__sys":1461227820801,"document_id":1051619},{"role_name__sys":"emea_skm_reviewer__c","user__sys":null,"group__sys":1461227848170,"document_id":1051619},{"role_name__sys":"us_all_users__c","user__sys":null,"group__sys":1461227861501,"document_id":1051619},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227829501,"document_id":1051619},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835695,"document_id":1051619},{"role_name__sys":"dam_librarian__c","user__sys":null,"group__sys":1461227871714,"document_id":1051619},{"role_name__sys":"business_admin_us__c","user__sys":null,"group__sys":1461227848976,"document_id":1051619},{"role_name__sys":"emea_scientific_department_3__c","user__sys":null,"group__sys":1461227847880,"document_id":1051619},{"role_name__sys":"approved_content_viewer__c","user__sys":null,"group__sys":1461227830527,"document_id":1051619},{"role_name__sys":"approved_content_with_annotation_viewer__c","user__sys":null,"group__sys":1461227876601,"document_id":1051619},{"role_name__sys":"approved_content_with_annotation_viewer__c","user__sys":null,"group__sys":1461227857404,"document_id":1051619},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227875206,"document_id":1051619}]}},{"id":1051674,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":3,"total":3},"data":[{"role_name__sys":"approved_email_content_admin__c","user__sys":null,"group__sys":1461227819301,"document_id":1051674},{"role_name__sys":"owner__c","user__sys":7990887,"group__sys":null,"document_id":1051674},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835717,"document_id":1051674}]}},{"id":1051711,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":10,"total":10},"data":[{"role_name__sys":"business_admin__c","user__sys":null,"group__sys":1461227836224,"document_id":1051711},{"role_name__sys":"reference_specialist__c","user__sys":null,"group__sys":1461227836222,"document_id":1051711},{"role_name__sys":"owner__c","user__sys":3600355,"group__sys":null,"document_id":1051711},{"role_name__sys":"approved_content_viewer__c","user__sys":null,"group__sys":1461227830518,"document_id":1051711},{"role_name__sys":"us_all_agency__c","user__sys":null,"group__sys":1461227848949,"document_id":1051711},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227829501,"document_id":1051711},{"role_name__sys":"all_us_users__c","user__sys":null,"group__sys":1461227848952,"document_id":1051711},{"role_name__sys":"approved_content_with_annotation_viewer__c","user__sys":null,"group__sys":1461227857405,"document_id":1051711},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227875206,"document_id":1051711},{"role_name__sys":"global_inbound_integration_user__c","user__sys":null,"group__sys":1461227890826,"document_id":1051711}]}},{"id":1052121,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":3,"total":3},"data":[{"role_name__sys":"approved_email_content_admin__c","user__sys":null,"group__sys":1461227826601,"document_id":1052121},{"role_name__sys":"owner__c","user__sys":8182289,"group__sys":null,"document_id":1052121},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835681,"document_id":1052121}]}},{"id":1052126,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":3,"total":3},"data":[{"role_name__sys":"approved_email_content_admin__c","user__sys":null,"group__sys":1461227826601,"document_id":1052126},{"role_name__sys":"owner__c","user__sys":8182289,"group__sys":null,"document_id":1052126},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835681,"document_id":1052126}]}},{"id":1052138,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":3,"total":3},"data":[{"role_name__sys":"approved_email_content_admin__c","user__sys":null,"group__sys":1461227826601,"document_id":1052138},{"role_name__sys":"owner__c","user__sys":8182289,"group__sys":null,"document_id":1052138},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835681,"document_id":1052138}]}},{"id":1052143,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":3,"total":3},"data":[{"role_name__sys":"approved_email_content_admin__c","user__sys":null,"group__sys":1461227826601,"document_id":1052143},{"role_name__sys":"owner__c","user__sys":8182289,"group__sys":null,"document_id":1052143},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835681,"document_id":1052143}]}},{"id":1052182,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":21,"total":21},"data":[{"role_name__sys":"emea_submitter__c","user__sys":2379637,"group__sys":null,"document_id":1052182},{"role_name__sys":"emea_other_reviewer__c","user__sys":2379637,"group__sys":null,"document_id":1052182},{"role_name__sys":"owner__c","user__sys":2446189,"group__sys":null,"document_id":1052182},{"role_name__sys":"emea_project_owner__c","user__sys":2446189,"group__sys":null,"document_id":1052182},{"role_name__sys":"distribution_contacts__c","user__sys":2446189,"group__sys":null,"document_id":1052182},{"role_name__sys":"business_admin_us__c","user__sys":null,"group__sys":1461227848992,"document_id":1052182},{"role_name__sys":"global_inbound_integration_user__c","user__sys":null,"group__sys":1461227874501,"document_id":1052182},{"role_name__sys":"approved_content_viewer__c","user__sys":null,"group__sys":1461227820801,"document_id":1052182},{"role_name__sys":"emea_signatories__c","user__sys":null,"group__sys":1461227847859,"document_id":1052182},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835662,"document_id":1052182},{"role_name__sys":"emea_scientific_department_3__c","user__sys":null,"group__sys":1461227848106,"document_id":1052182},{"role_name__sys":"emea_scientific_department_1__c","user__sys":null,"group__sys":1461227847862,"document_id":1052182},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227829501,"document_id":1052182},{"role_name__sys":"emea_medical_reviewer__c","user__sys":null,"group__sys":1461227848119,"document_id":1052182},{"role_name__sys":"dam_librarian__c","user__sys":null,"group__sys":1461227871686,"document_id":1052182},{"role_name__sys":"approved_content_viewer__c","user__sys":null,"group__sys":1461227830527,"document_id":1052182},{"role_name__sys":"approved_content_with_annotation_viewer__c","user__sys":null,"group__sys":1461227876601,"document_id":1052182},{"role_name__sys":"emea_skm_reviewer__c","user__sys":null,"group__sys":1461227853617,"document_id":1052182},{"role_name__sys":"approved_content_with_annotation_viewer__c","user__sys":null,"group__sys":1461227857404,"document_id":1052182},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227875206,"document_id":1052182},{"role_name__sys":"emea_scientific_department_2__c","user__sys":null,"group__sys":1461227847877,"document_id":1052182}]}},{"id":1052240,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":3,"total":3},"data":[{"role_name__sys":"approved_email_content_admin__c","user__sys":null,"group__sys":1461227826603,"document_id":1052240},{"role_name__sys":"owner__c","user__sys":8182289,"group__sys":null,"document_id":1052240},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835657,"document_id":1052240}]}},{"id":1052242,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":21,"total":21},"data":[{"role_name__sys":"emea_project_owner__c","user__sys":7049111,"group__sys":null,"document_id":1052242},{"role_name__sys":"emea_submitter__c","user__sys":7049111,"group__sys":null,"document_id":1052242},{"role_name__sys":"emea_other_reviewer__c","user__sys":7049111,"group__sys":null,"document_id":1052242},{"role_name__sys":"distribution_contacts__c","user__sys":7049111,"group__sys":null,"document_id":1052242},{"role_name__sys":"business_admin_us__c","user__sys":null,"group__sys":1461227848992,"document_id":1052242},{"role_name__sys":"global_inbound_integration_user__c","user__sys":null,"group__sys":1461227874501,"document_id":1052242},{"role_name__sys":"approved_content_viewer__c","user__sys":null,"group__sys":1461227820801,"document_id":1052242},{"role_name__sys":"emea_signatories__c","user__sys":null,"group__sys":1461227847859,"document_id":1052242},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835662,"document_id":1052242},{"role_name__sys":"emea_scientific_department_3__c","user__sys":null,"group__sys":1461227848106,"document_id":1052242},{"role_name__sys":"emea_scientific_department_1__c","user__sys":null,"group__sys":1461227847862,"document_id":1052242},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227829501,"document_id":1052242},{"role_name__sys":"emea_medical_reviewer__c","user__sys":null,"group__sys":1461227848119,"document_id":1052242},{"role_name__sys":"dam_librarian__c","user__sys":null,"group__sys":1461227871686,"document_id":1052242},{"role_name__sys":"approved_content_viewer__c","user__sys":null,"group__sys":1461227830527,"document_id":1052242},{"role_name__sys":"approved_content_with_annotation_viewer__c","user__sys":null,"group__sys":1461227876601,"document_id":1052242},{"role_name__sys":"emea_skm_reviewer__c","user__sys":null,"group__sys":1461227853617,"document_id":1052242},{"role_name__sys":"approved_content_with_annotation_viewer__c","user__sys":null,"group__sys":1461227857404,"document_id":1052242},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227875206,"document_id":1052242},{"role_name__sys":"emea_scientific_department_2__c","user__sys":null,"group__sys":1461227847877,"document_id":1052242},{"role_name__sys":"owner__c","user__sys":9085189,"group__sys":null,"document_id":1052242}]}}

You don't really need RecursiveIteratorIterator or any of those jazz as it will make your solution look more complicated. Just add the headers as the first row and get & put the rows in the CSV by accessing the subarrays inside data key.
Snippet:
<?php
$json = <<<JSON
[
{"id":1051524,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":20,"total":20},"data":[{"role_name__sys":"owner__c","user__sys":8401011,"group__sys":null,"document_id":1051524},{"role_name__sys":"emea_project_owner__c","user__sys":8401011,"group__sys":null,"document_id":1051524},{"role_name__sys":"emea_submitter__c","user__sys":8401011,"group__sys":null,"document_id":1051524},{"role_name__sys":"distribution_contacts__c","user__sys":8401011,"group__sys":null,"document_id":1051524},{"role_name__sys":"business_admin_us__c","user__sys":null,"group__sys":1461227848992,"document_id":1051524},{"role_name__sys":"global_inbound_integration_user__c","user__sys":null,"group__sys":1461227874501,"document_id":1051524},{"role_name__sys":"approved_content_viewer__c","user__sys":null,"group__sys":1461227820801,"document_id":1051524},{"role_name__sys":"emea_signatories__c","user__sys":null,"group__sys":1461227847859,"document_id":1051524},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835662,"document_id":1051524},{"role_name__sys":"emea_scientific_department_3__c","user__sys":null,"group__sys":1461227848106,"document_id":1051524},{"role_name__sys":"emea_scientific_department_1__c","user__sys":null,"group__sys":1461227847862,"document_id":1051524},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227829501,"document_id":1051524},{"role_name__sys":"emea_medical_reviewer__c","user__sys":null,"group__sys":1461227848119,"document_id":1051524},{"role_name__sys":"dam_librarian__c","user__sys":null,"group__sys":1461227871686,"document_id":1051524},{"role_name__sys":"approved_content_viewer__c","user__sys":null,"group__sys":1461227830527,"document_id":1051524},{"role_name__sys":"approved_content_with_annotation_viewer__c","user__sys":null,"group__sys":1461227876601,"document_id":1051524},{"role_name__sys":"emea_skm_reviewer__c","user__sys":null,"group__sys":1461227853617,"document_id":1051524},{"role_name__sys":"approved_content_with_annotation_viewer__c","user__sys":null,"group__sys":1461227857404,"document_id":1051524},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227875206,"document_id":1051524},{"role_name__sys":"emea_scientific_department_2__c","user__sys":null,"group__sys":1461227847877,"document_id":1051524}]}},{"id":1051619,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":23,"total":23},"data":[{"role_name__sys":"owner__c","user__sys":3596955,"group__sys":null,"document_id":1051619},{"role_name__sys":"emea_project_owner__c","user__sys":3596955,"group__sys":null,"document_id":1051619},{"role_name__sys":"emea_submitter__c","user__sys":3596955,"group__sys":null,"document_id":1051619},{"role_name__sys":"emea_other_reviewer__c","user__sys":2285381,"group__sys":null,"document_id":1051619},{"role_name__sys":"emea_project_owner__c","user__sys":9395329,"group__sys":null,"document_id":1051619},{"role_name__sys":"distribution_contacts__c","user__sys":9395329,"group__sys":null,"document_id":1051619},{"role_name__sys":"emea_medical_reviewer__c","user__sys":null,"group__sys":1461227847879,"document_id":1051619},{"role_name__sys":"emea_signatories__c","user__sys":null,"group__sys":1461227847872,"document_id":1051619},{"role_name__sys":"emea_scientific_department_1__c","user__sys":null,"group__sys":1461227848105,"document_id":1051619},{"role_name__sys":"emea_scientific_department_2__c","user__sys":null,"group__sys":1461227847867,"document_id":1051619},{"role_name__sys":"global_inbound_integration_user__c","user__sys":null,"group__sys":1461227874501,"document_id":1051619},{"role_name__sys":"approved_content_viewer__c","user__sys":null,"group__sys":1461227820801,"document_id":1051619},{"role_name__sys":"emea_skm_reviewer__c","user__sys":null,"group__sys":1461227848170,"document_id":1051619},{"role_name__sys":"us_all_users__c","user__sys":null,"group__sys":1461227861501,"document_id":1051619},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227829501,"document_id":1051619},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835695,"document_id":1051619},{"role_name__sys":"dam_librarian__c","user__sys":null,"group__sys":1461227871714,"document_id":1051619},{"role_name__sys":"business_admin_us__c","user__sys":null,"group__sys":1461227848976,"document_id":1051619},{"role_name__sys":"emea_scientific_department_3__c","user__sys":null,"group__sys":1461227847880,"document_id":1051619},{"role_name__sys":"approved_content_viewer__c","user__sys":null,"group__sys":1461227830527,"document_id":1051619},{"role_name__sys":"approved_content_with_annotation_viewer__c","user__sys":null,"group__sys":1461227876601,"document_id":1051619},{"role_name__sys":"approved_content_with_annotation_viewer__c","user__sys":null,"group__sys":1461227857404,"document_id":1051619},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227875206,"document_id":1051619}]}},{"id":1051674,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":3,"total":3},"data":[{"role_name__sys":"approved_email_content_admin__c","user__sys":null,"group__sys":1461227819301,"document_id":1051674},{"role_name__sys":"owner__c","user__sys":7990887,"group__sys":null,"document_id":1051674},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835717,"document_id":1051674}]}},{"id":1051711,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":10,"total":10},"data":[{"role_name__sys":"business_admin__c","user__sys":null,"group__sys":1461227836224,"document_id":1051711},{"role_name__sys":"reference_specialist__c","user__sys":null,"group__sys":1461227836222,"document_id":1051711},{"role_name__sys":"owner__c","user__sys":3600355,"group__sys":null,"document_id":1051711},{"role_name__sys":"approved_content_viewer__c","user__sys":null,"group__sys":1461227830518,"document_id":1051711},{"role_name__sys":"us_all_agency__c","user__sys":null,"group__sys":1461227848949,"document_id":1051711},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227829501,"document_id":1051711},{"role_name__sys":"all_us_users__c","user__sys":null,"group__sys":1461227848952,"document_id":1051711},{"role_name__sys":"approved_content_with_annotation_viewer__c","user__sys":null,"group__sys":1461227857405,"document_id":1051711},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227875206,"document_id":1051711},{"role_name__sys":"global_inbound_integration_user__c","user__sys":null,"group__sys":1461227890826,"document_id":1051711}]}},{"id":1052121,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":3,"total":3},"data":[{"role_name__sys":"approved_email_content_admin__c","user__sys":null,"group__sys":1461227826601,"document_id":1052121},{"role_name__sys":"owner__c","user__sys":8182289,"group__sys":null,"document_id":1052121},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835681,"document_id":1052121}]}},{"id":1052126,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":3,"total":3},"data":[{"role_name__sys":"approved_email_content_admin__c","user__sys":null,"group__sys":1461227826601,"document_id":1052126},{"role_name__sys":"owner__c","user__sys":8182289,"group__sys":null,"document_id":1052126},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835681,"document_id":1052126}]}},{"id":1052138,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":3,"total":3},"data":[{"role_name__sys":"approved_email_content_admin__c","user__sys":null,"group__sys":1461227826601,"document_id":1052138},{"role_name__sys":"owner__c","user__sys":8182289,"group__sys":null,"document_id":1052138},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835681,"document_id":1052138}]}},{"id":1052143,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":3,"total":3},"data":[{"role_name__sys":"approved_email_content_admin__c","user__sys":null,"group__sys":1461227826601,"document_id":1052143},{"role_name__sys":"owner__c","user__sys":8182289,"group__sys":null,"document_id":1052143},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835681,"document_id":1052143}]}},{"id":1052182,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":21,"total":21},"data":[{"role_name__sys":"emea_submitter__c","user__sys":2379637,"group__sys":null,"document_id":1052182},{"role_name__sys":"emea_other_reviewer__c","user__sys":2379637,"group__sys":null,"document_id":1052182},{"role_name__sys":"owner__c","user__sys":2446189,"group__sys":null,"document_id":1052182},{"role_name__sys":"emea_project_owner__c","user__sys":2446189,"group__sys":null,"document_id":1052182},{"role_name__sys":"distribution_contacts__c","user__sys":2446189,"group__sys":null,"document_id":1052182},{"role_name__sys":"business_admin_us__c","user__sys":null,"group__sys":1461227848992,"document_id":1052182},{"role_name__sys":"global_inbound_integration_user__c","user__sys":null,"group__sys":1461227874501,"document_id":1052182},{"role_name__sys":"approved_content_viewer__c","user__sys":null,"group__sys":1461227820801,"document_id":1052182},{"role_name__sys":"emea_signatories__c","user__sys":null,"group__sys":1461227847859,"document_id":1052182},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835662,"document_id":1052182},{"role_name__sys":"emea_scientific_department_3__c","user__sys":null,"group__sys":1461227848106,"document_id":1052182},{"role_name__sys":"emea_scientific_department_1__c","user__sys":null,"group__sys":1461227847862,"document_id":1052182},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227829501,"document_id":1052182},{"role_name__sys":"emea_medical_reviewer__c","user__sys":null,"group__sys":1461227848119,"document_id":1052182},{"role_name__sys":"dam_librarian__c","user__sys":null,"group__sys":1461227871686,"document_id":1052182},{"role_name__sys":"approved_content_viewer__c","user__sys":null,"group__sys":1461227830527,"document_id":1052182},{"role_name__sys":"approved_content_with_annotation_viewer__c","user__sys":null,"group__sys":1461227876601,"document_id":1052182},{"role_name__sys":"emea_skm_reviewer__c","user__sys":null,"group__sys":1461227853617,"document_id":1052182},{"role_name__sys":"approved_content_with_annotation_viewer__c","user__sys":null,"group__sys":1461227857404,"document_id":1052182},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227875206,"document_id":1052182},{"role_name__sys":"emea_scientific_department_2__c","user__sys":null,"group__sys":1461227847877,"document_id":1052182}]}},{"id":1052240,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":3,"total":3},"data":[{"role_name__sys":"approved_email_content_admin__c","user__sys":null,"group__sys":1461227826603,"document_id":1052240},{"role_name__sys":"owner__c","user__sys":8182289,"group__sys":null,"document_id":1052240},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835657,"document_id":1052240}]}},{"id":1052242,"doc_roles__sysr":{"responseDetails":{"pagesize":250,"pageoffset":0,"size":21,"total":21},"data":[{"role_name__sys":"emea_project_owner__c","user__sys":7049111,"group__sys":null,"document_id":1052242},{"role_name__sys":"emea_submitter__c","user__sys":7049111,"group__sys":null,"document_id":1052242},{"role_name__sys":"emea_other_reviewer__c","user__sys":7049111,"group__sys":null,"document_id":1052242},{"role_name__sys":"distribution_contacts__c","user__sys":7049111,"group__sys":null,"document_id":1052242},{"role_name__sys":"business_admin_us__c","user__sys":null,"group__sys":1461227848992,"document_id":1052242},{"role_name__sys":"global_inbound_integration_user__c","user__sys":null,"group__sys":1461227874501,"document_id":1052242},{"role_name__sys":"approved_content_viewer__c","user__sys":null,"group__sys":1461227820801,"document_id":1052242},{"role_name__sys":"emea_signatories__c","user__sys":null,"group__sys":1461227847859,"document_id":1052242},{"role_name__sys":"global_integration_user__c","user__sys":null,"group__sys":1461227835662,"document_id":1052242},{"role_name__sys":"emea_scientific_department_3__c","user__sys":null,"group__sys":1461227848106,"document_id":1052242},{"role_name__sys":"emea_scientific_department_1__c","user__sys":null,"group__sys":1461227847862,"document_id":1052242},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227829501,"document_id":1052242},{"role_name__sys":"emea_medical_reviewer__c","user__sys":null,"group__sys":1461227848119,"document_id":1052242},{"role_name__sys":"dam_librarian__c","user__sys":null,"group__sys":1461227871686,"document_id":1052242},{"role_name__sys":"approved_content_viewer__c","user__sys":null,"group__sys":1461227830527,"document_id":1052242},{"role_name__sys":"approved_content_with_annotation_viewer__c","user__sys":null,"group__sys":1461227876601,"document_id":1052242},{"role_name__sys":"emea_skm_reviewer__c","user__sys":null,"group__sys":1461227853617,"document_id":1052242},{"role_name__sys":"approved_content_with_annotation_viewer__c","user__sys":null,"group__sys":1461227857404,"document_id":1052242},{"role_name__sys":"portal_manager__c","user__sys":null,"group__sys":1461227875206,"document_id":1052242},{"role_name__sys":"emea_scientific_department_2__c","user__sys":null,"group__sys":1461227847877,"document_id":1052242},{"role_name__sys":"owner__c","user__sys":9085189,"group__sys":null,"document_id":1052242}]}}
]
JSON;
$content = json_decode($json, true);
$fp = fopen('doc_roles__sys.csv', 'w+');
if($fp === false){
throw new \Exception("CSV file could not be created!");
}
fputcsv($fp, explode(",", "id,role_name__sys,user__sys,group__sys,document_id"));
foreach($content as $c){
foreach($c['doc_roles__sysr']['data'] as $row){
fputcsv($fp, [
$row['document_id'],
$row['role_name__sys'],
$row['user__sys'],
$row['group__sys'],
$row['document_id']
]);
}
}
fclose($fp);

Related

PHP Group keys of associative array by values

function groupByOwners(array $files) : array { return []; }
$files = array("Input.txt" => "Randy","Code.py" => "Stan","Output.txt" =>"Randy");
Print_r(groupByOwners($files);
My expected output is:
[Randy => [Input.txt, Output.txt] , Stan => [Code.py]]
You just need to iterate over your array, pushing each filename to a new array indexed by the names:
function groupByOwners(array $files) : array {
$output = array();
foreach ($files as $file => $name) {
$output[$name][] = $file;
}
return $output;
}
$files = array("Input.txt" => "Randy","Code.py" => "Stan","Output.txt" =>"Randy");
print_r(groupByOwners($files));
Output:
Array
(
[Randy] => Array
(
[0] => Input.txt
[1] => Output.txt
)
[Stan] => Array
(
[0] => Code.py
)
)
Demo on 3v4l.org

How to separate csv array by column?

I have a giant list in excel, they are just two columns
name type
The file is currently being read:
$lines = array_map('str_getcsv', file('file.csv', FILE_IGNORE_NEW_LINES));
print_r($lines); returns:
name1;type
name2;type2
...
Array ( [0] => Array ( [0] => name1;type1) [1] => Array ( [0] => name2;type2)...
I would like to access separate name and type in an foreach
How can I do this?
Thanks
str_getcsv default delimiter is ',' so you need call it somehow with explicitly specifying ';' as delimeter
for example like this
$myGetCsv = function ($str) {
return str_getcsv($str, ';');
};
$lines = array_map($myGetCsv, file('file.csv', FILE_IGNORE_NEW_LINES));
Use this code for CSV file reading. it ease to use and understand how its work.
if (($handle = fopen('./suppression_invalid_emails.csv', "r")) !== false) {
//getting column name
$column_headers = fgetcsv($handle);
foreach ($column_headers as $header) {
//column as array;
$result[$header] = array();
}
// get data for column;
while (($data = fgetcsv($handle)) !== false) {
$i = 0;
foreach ($result as &$column) {
$column[] = $data[$i++];
}
}
fclose($handle);
echo '<pre>'; print_r($result); echo '</pre>';
}
i use two basic function for this 1st one is fopen for reading file and other is fgetcsv for getting data for column.
and result is:
Array
(
[reason] => Array
(
[0] => Mail domain mentioned in email address is unknown
[1] => Known bad domain
[2] => Known bad domain
)
[email] => Array
(
[0] => muneque#collegeclub.om
[1] => saharfa2000#hatmail.com
[2] => tawheeda81#yahoo.co
)
[created] => Array
(
[0] => 1502644294
[1] => 1502480171
[2] => 1502344320
)
)

Convert CSV into Array

I have a csv file that looks like this:
Did,status
"123","Active"
"456","Not-Active"
"789","Active"
....and so on
I would like to be able to convert it into an array that looks like this:
$DidStatus = array("123"=>"Active", "456"=>"Not-Active", "789"=>"Active");
I tried this but it's not what I'm looking for:
$file = file_get_contents("test.csv");
$data = array_map("str_getcsv", preg_split('/\r*\n+|\r+/', $file));
print_r($data);
but the output is not the one I'm looking for:
Array
(
[0] => Array
(
[0] => Did
[1] => status
)
[1] => Array
(
[0] => 123
[1] => Active
)
[2] => Array
(
[0] => 456
[1] => Not-Active
)
[3] => Array
(
[0] => 789
[1] => Active
)
[4] => Array
(
[0] =>
)
)
Look into fgetcsv()
<?php
$handle = fopen("test.csv", "r");
$result = Array();
fgetcsv($handle); //Removes the first line of headings in the csv
while($data = fgetcsv($handle)) {
$result[$data[0]] = $data[1];
}
print_r($result); //the result
?>
There are other ways to do it, but given your current code, just extract an array of the 1 values and index it by the 0 values.
unset($data[0]); //If needed to remove the header row
$data = array_column($data, 1, 0);
You may consider this as an alternate for the first step (not sure if FILE_IGNORE_NEW_LINES is absolutely necessary):
$data = array_map('str_getcsv', file('test.csv', FILE_IGNORE_NEW_LINES));
Look into fgetcsv. That is meant to be used for something like this.
$arr = array();
$file = fopen('test.csv', 'r');
while (($result = fgetcsv($file)) !== FALSE)
{
$arr[] = $result;
}
fclose($file);
You can use this package league/csv and you can find instructions on how to use it here - one of the first examples shows how to convert csv to array

How can i fetch a value in this array (php)

i have a file with this array content (bottom) , how can i get a value ?
Array
(
[id_product] => 29438.199
[mojoodi] => 29.000000
[price] => 20000.0000
)
Array
(
[id_product] => 25912.199
[mojoodi] => 72.000000
[price] => 215000.0000
)
Array
(
[id_product] => 24617.199
[mojoodi] => 22.000000
[price] => 34900.0000
)
my code for put data in the file is :
$myfile = fopen("test.php", "w") or die("Unable to open file!");
for($i =0;$i<=50;$i++) {
$data = array("id_product"=>"$product_ids[$i]", "mojoodi"=>"$mojoodi[$i]", "price"=>"$price[$i]");
$results = print_r($data, true);
fwrite($myfile, $results);
}
fclose($myfile);
Don't use print_r() if you need to parse the data. Use JSON or serialize.
$array = array();
for($i =0;$i<=50;$i++) {
$array[] = array("id_product"=>"$product_ids[$i]", "mojoodi"=>"$mojoodi[$i]", "price"=>"$price[$i]");
}
file_put_contents("test.data", serialize($array));
Then when you want to read it back, you do:
$array = unserialize(file_get_contents("test.data"));
First of all your are overwritting $data by assigning value again and again to change the following line
$data = array("id_product"=>"$product_ids[$i]", "mojoodi"=>"$mojoodi[$i]", "price"=>"$price[$i]");
into
$data[] = array("id_product"=>"$product_ids[$i]", "mojoodi"=>"$mojoodi[$i]", "price"=>"$price[$i]");
Then you can get 2nd array's price index via following code.
echo $data[1]['price'];

Loop through a CSV file

How can i do a foreach with a CSV file.
Suppose i have fopen a csv file which looks like so ---
Array
(
[0] => Category ID
[1] => Category
[2] => Country
[3] => Original price
[4] => Reduce price
)
Array
(
[0] => 11
[1] => Phone cases
[2] => Denmark
[3] => 90,99
[4] => 120
)
Array
(
[0] => 13
[1] => Shoes
[2] => Denmark
[3] => 180,99
[4] => 200
)
So how can i loop through all the data's in store them in a array.
I have tried like so, but it does not work.
$all_data = array();
foreach ($result as $key => $obj) {
$doc = array();
$doc['id'] = $obj[$key];
$doc['category'] = $obj[$key];
$doc['country'] = $obj[$key];
$doc['original_price'] = $obj[$key];
$doc['reduce_price'] = $obj[$key];
// array push all documents into $all_data
$all_data [] = $doc;
}
Anyone knows how i can loop through those data!
Just using the PHP documentation :
if (($handle = fopen("yourfile.csv", "r")) !== FALSE) {
$all_data = array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($i==0){ continue; $i=1; }
// Remove the first iteration as it's not "real" datas
$all_data[] = array(
'id' => $data[0],
'category' => $data[1],
'country' => $data[2],
'original_price' => $data[3],
'reduce_price' => $data[4],
);
}
fclose($handle);
}
It must achieve what you want. It's always better using the built-in functions. If you can't use this one, let me know.
EDIT :
As #Blag said :
you can use to file_get_contents() and str_getcsv()
But this is a different approach that'll load the full file in a string (file_get_contents) and parse the string in the second time (str_getcsv).
This should give you an idea of how to proceed :
$objsOut=array();
foreach ($result as $key => $obj) {
$objOut = array(
'id'=>$obj[0]
);
$objsOut[]=$objOut;
}

Categories