Home > Enterprise >  How to add headers while converting JSON to CSV in PHP?
How to add headers while converting JSON to CSV in PHP?

Time:09-20

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}]}}

CodePudding user response:

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