How can i fetch a value in this array (php) - 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'];

Related

How to add headers while converting JSON to CSV in 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);

Read data from CSV into a single array or a single json

I am using jQuery autocomplete function. For it to work I need the json in this format ["Alaska","Alabama","Arkansas"] . Currently I am reading the data from a CSV file. This is my code.
$f = fopen($filename, "r");
while ($row = fgetcsv($f)) {
$out = $row[2];
print_r (explode(" , ",$out));
}
Now I get the output as
Array
(
[0] => Alaska
)
Array
(
[0] => Alabama
)
Array
(
[0] => Arkansas
)
And my Json as
["Alaska"]["Romeo"]["Arkansas"]
How do I convert the array into a simple array like
Array
(
[0] => Alaska
[1] => Alabama
[2] => Arkansas
)
If not into a json like this
["Alaska","Alabama","Arkansas"]
You build an array of values and then put them in json_encode() function.
$data = [];
$f = fopen($filename, "r");
while ($row = fgetcsv($f)) {
$out = $row[2];
$data = array_merge($data, explode(" , ",$out)));
}
echo json_encode($data);
Might be easier to read the file into an arary, map to a function that explodes, then encode and display:
echo json_encode(
array_map(function($v) { return explode(" , ", $v)[2]; }, file($filename))
);

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

php multi-dimensional array provides three levels of depth instead of two

When I execute the code below:
Code:
<?php
$data = array();
$jim = array('Jim'=>1);
$bob = array('Bob'=>1);
$data['abc'][] = $jim;
$data['abc'][] = $bob;
print_r($data);
?>
I receive the following output:
Array
(
[abc] => Array
(
[0] => Array
(
[Jim] => 1
)
[1] => Array
(
[Bob] => 1
)
)
)
What I am expecting is the following output:
Array
(
[abc] => Array
(
[Jim] => 1
[Bob] => 1
)
)
How can I achieve this? To rephrase the question, how can I keep it to a single sub-array per a supplied key?
$data = array();
$jim = array('Jim'=>1);
$bob = array('Bob'=>1);
$data['abc'] = array_merge($jim, $bob);
print_r($data);
You are creating array ($data['abc']) which contains an array ([]) of arrays($jim, $bob)
It's the same as writing:
$data['abc'][0] = array('jim' => 1);
$data['abc'][1] = array('bob' => 1);
What you want is probably:
$data['abc'] = array();
$data['abc'] = array_merge($data['abc'], $jim, $bob);
Jim and Bob are array indexes by your own declaration, you have to change them first
<?php
$data = array();
$data['abc']["Jim"] =1;
$data['abc']["Bob"] = 2;
print_r($data);
?>
Demo

How to get the url and put them in a text file one line by one line?

supposed there is a variable named:
src="http://www.test.com/1455/"||src="http://www.test.com/image/1456/"||
now i want to get the url part http://www.test.com/1455/.
the following is my code.but it doesn't work.
$url = 'src="http://www.test.com/1455/"||src="http://www.test.com/image/1456/"||';
$a = explode("src=\"",$url);
$b = explode("||",$a);
$handle = fopen("output.txt", 'a')) !== FALSE
fwrite($handle2, "{$b[0]}\n");
Use regular expression something similar to this, of course you can modify this as you needed. This is just an example but it will work for you
$url = 'src="http://www.test.com/1455/"||src="http://www.test.com/image/1456/"||';
preg_match_all('/\b(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&##\/%=~_|$?!:,.]*[A-Z0-9+&##\/%=~_|$]/i', $url, $result, PREG_PATTERN_ORDER);
print_R($result);
it will print
Array
(
[0] => Array
(
[0] => http://www.test.com/1455/
[1] => http://www.test.com/image/1456/
)
)
and write this in text file:
$handle = fopen("output.txt", 'a');
foreach($result[0] as $url){
fwrite($handle, $url."\n");
}
fclose($handle);
more details on regex here http://php.net/manual/en/function.preg-match.php
Solution for the more general case. This code converts your string into URL query format and parse it via parse_str function
$url = 'src="http://www.test.com/1455/"||src="http://www.test.com/image/1456/"||var1=aaa||var2=bbb||var2=bbb';
$url = str_replace('||', '&', $url);
$url = preg_replace('/([^\]])=/', '$1[]=', $url);
$params = array();
parse_str($url, $params);
print_r($params);
Output:
Array
(
[src] => Array
(
[0] => "http://www.test.com/1455/"
[1] => "http://www.test.com/image/1456/"
)
[var1] => Array
(
[0] => aaa
)
[var2] => Array
(
[0] => bbb
[1] => bbb
)
)
Then you can do with this array everything you want
$url = 'src="http://www.test.com/1455/"||src="http://www.test.com/image/1456/"||';
$a = explode("||",$url);
$handle = fopen("output.txt", 'a');
foreach($a as &$val)
{
$val = str_replace("src=\"","",$val);
$val = str_replace("\"","",$val);
fwrite($handle, "{$val}\r\n");
}

Categories