Loop through a CSV file - php

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

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);

counting row values with different ids in multidimensional array

I know there are a lot of topics about this, but just couldn't find any that would actually help me solve the problem.
Ok so i am having a problem with counting rows and values in multidimensional array (imported via .csv)
I am trying to achieve something like...
Array ( [0] => Array ( [0] => 1 [1] => 6278,31 [2] => 4)
[1] => Array ( [0] => 2 [1] => 2,0 [2] => 2)
[2] => Array ( [0] => 3 [1] => 3,01 [2] => 3)
)
where [0] would be buyer id, 1 is total number of buys and [2] total cost.
My current code:
if(($s_open = fopen("sales.csv", "r")) !== FALSE)
{
$s_count = count(file("sales.csv"));
while(($sales = fgetcsv($s_open, 100, ";")) !== FALSE)
{
...
}
fclose($s_open);
}
Thanks in advance.
You could group your data by creating an array with the buyer_id as index.
Each time you find a new buyer, you could create a small array ('id','count','amount').
Then, sum the values of the current row into that array.
const KEY_ID = 0; // shortcut for array indexes
const KEY_AMOUNT = 1;
const KEY_COUNT = 2;
$buyers = []; // final array
if (($s_open = fopen("sales.csv", "r")) !== FALSE)
{
$s_count = count(file("sales.csv"));
while(($sales = fgetcsv($s_open, 100, ";")) !== FALSE)
{
// Extract data from row:
[$id, $buyer_id, , $amount] = $sales;
// If the buyer doesn't exists in the array,
// create a new array with required format:
if (!isset($buyers[$buyer_id])) {
$buyers[$buyer_id] = [
KEY_ID => $buyer_id,
KEY_COUNT => 0,
KEY_AMOUNT => 0,
];
}
// Update values:
$buyers[$buyer_id][KEY_COUNT]++;
$buyers[$buyer_id][KEY_AMOUNT] += $amount;
}
fclose($s_open);
}
// Re-index values:
$buyers = array_values($buyers);
Using this mock data in.csv:
id, buyer_id, amount
1, 1, 100.55
2, 1, 500.1
3, 2, 50.55
4, 3, 1.0
I wrote the following PHP to parse it into a nicely formatted array (if you don't want to have named keys, you can just remove the header logic):
<?php
// Each line into an array
$csv = file(__DIR__ . DIRECTORY_SEPARATOR . 'in.csv');
// First line is the header
$header = str_getcsv(array_shift($csv));
// When the CSV header is `id, buyer_id, amount`, remove the whitespace
$header = array_map(function ($item) {
return trim($item);
}, $header);
// Get the CSV data as an array of arrays
$out = array_map(function ($line) use ($header) {
$data = str_getcsv($line);
return array_combine($header, $data);
}, $csv);
print_r($out);
Which prints the following to your screen:
Array
(
[0] => Array
(
[id] => 1
[buyer_id] => 1
[amount] => 100.55
)
[1] => Array
(
[id] => 2
[buyer_id] => 1
[amount] => 500.1
)
[2] => Array
(
[id] => 3
[buyer_id] => 2
[amount] => 50.55
)
[3] => Array
(
[id] => 4
[buyer_id] => 3
[amount] => 1.0
)
)
Try this
$data = [];
if(($s_open = fopen("sales.csv", "r")) !== FALSE) {
$s_count = count(file("sales.csv"));
while( ([$id, $buyer_id, $amount] = fgetcsv($s_open, 100, ";")) !== FALSE ){
if( $id !== 'id' ){
$data[$buyer_id] = [
$buyer_id,
($data[$buyer_id][1] ?? 0) + 1,
($data[$buyer_id][2] ?? 0) + $amount
];
}
}
fclose($s_open);
$data = array_values($data);
}
// print_r($data);

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
)
)

PHP - Fastest way to convert a 2d array into a 3d array that is grouped by a specific value

I would like to convert this two dimensional array of records:
[records] => Array
(
[0] => Array
(
[0] => Pears
[1] => Green
[2] => Box
[3] => 20
)
[1] => Array
(
[0] => Pears
[1] => Yellow
[2] => Packet
[3] => 4
)
[2] => Array
(
[0] => Peaches
[1] => Orange
[2] => Packet
[3] => 4
)
[3] => Array
(
[0] => Apples
[1] => Red
[2] => Box
[3] => 20
)
)
Into this three dimensional array where each array key is grouped by a certain value from the original array:
[converted_records] => Array
(
[Pears] => Array
(
[0] => Array
(
[0] => Green
[1] => Box
[2] => 20
)
[1] => Array
(
[0] => Yellow
[1] => Packet
[2] => 4
)
)
[Peaches] => Array
(
[0] => Array
(
[0] => Orange
[1] => Packet
[2] => 4
)
)
[Apples] => Array
(
[0] => Array
(
[0] => Red
[1] => Box
[2] => 20
)
)
)
I can do this like so:
$array = // Sample data like the first array above
$storage = array();
$cnt = 0;
foreach ($array as $key=>$values) {
$storage[$values[0]][$cnt] = array (
0 => $values[1],
1 => $values[2],
2 => $values[3]
);
$cnt ++;
}
I wanted to know if there is a more optimal way to do this. I am not aware of any functions within PHP that are capable of this so I can only assume that this is basically how it would be done.
The problem is though, this is going to be repeated so so many times and every little millisecond is going to count so I really want to know what is the best way to accomplish this task?
EDIT
The records array is created by parsing a .CSV file as follows:
$records = array_map('str_getcsv', file('file.csv'));
EDIT #2
I did a simple benchmark test on a set of 10 results (5k records each) to get an average runtime of 0.645478 seconds. Granted there is a few other things going on before this so this is not a true indication of actual performance but a good indication for comparison to other methods.
EDIT #3
I did a test with about 20x the records. The average of my routine was 14.91971.
At some point the answer below by #num8er had $records[$key][] = array_shift($data); before updating the answer as it is now.
When I tried testing with the larger set of results this it ran out of memory as its generating an error for each record.
This being said, once i did $records[$key][] = $data; the routine completed with an average of 18.03699 seconds with gc_collect_cycles() commented out.
I've reached the conclusion that although #num8ers method is faster for smaller files, for larger ones my method works out quicker.
If you're only looking for some clean code:
$array = array_map('str_getcsv', file('file.csv'));
$storage = array();
foreach ($array as $values) {
$key = array_shift($values);
$storage[$key][] = $values;
}
Unless you have hundreds of thousands of array entries, speed shouldnt be a concern either.
reading big file to memory using file() (1st iteration when it reads file)
and then iterating lines using array_map (2nd iteration after each line of file is read to array)
doing foreach on array (3rd iteration)
it is bad idea when You're looking for performance.
You're iterating 3 times. so what about 100K records? it will iterate 300K times?
most performant way is to do it while reading file. there is only 1 iteration - reading lines (100K records == 100K iteration):
ini_set('memory_limit', '1024M');
set_time_limit(0);
$file = 'file.csv';
$file = fopen($file, 'r');
$records = array();
while($data = fgetcsv($file)) {
$key = $data[0];
if(!isset($records[$key])) {
$records[$key] = array();
}
$records[$key][] = array(0 => $data[1],
1 => $data[2],
2 => $data[3]);
gc_collect_cycles();
}
fclose($file);
and here is parent -> children processing for huge files:
<?php
ini_set('memory_limit', '1024M');
set_time_limit(0);
function child_main($file)
{
$my_pid = getmypid();
print "Starting child pid: $my_pid\n";
/**
* OUR ROUTINE
*/
$file = fopen($file, 'r');
$records = array();
while($data = fgetcsv($file)) {
$key = $data[0];
if(!isset($records[$key])) {
$records[$key] = array();
}
$records[$key][] = array(0 => $data[1],
1 => $data[2],
2 => $data[3]);
gc_collect_cycles();
}
fclose($file);
unlink($file);
return 1;
}
$file = __DIR__."/file.csv";
$files = glob(__DIR__.'/part_*');
if(sizeof($files)==0) {
exec('split -l 1000 '.$file.' part_');
$files = glob(__DIR__.'/part_*');
}
$children = array();
foreach($files AS $file) {
if(($pid = pcntl_fork()) == 0) {
exit(child_main($file));
}
else {
$children[] = $pid;
}
}
foreach($children as $pid) {
$pid = pcntl_wait($status);
if(pcntl_wifexited($status)) {
$code = pcntl_wexitstatus($status);
print "pid $pid returned exit code: $code\n";
}
else {
print "$pid was unnaturally terminated\n";
}
}
?>

Import CSV data into MySQL

Consider the following snippet CSV data from "NASDAQ.csv"
"Symbol,""Name"",""LastSale"",""MarketCap"",""ADR TSO"",""IPOyear"",""Sector"",""industry"",""Summary Quote"",";;
"FLWS,""1-800 FLOWERS.COM, Inc."",""2.9"",""81745200"",""n/a"",""1999"",""Consumer Services"",""Other Specialty Stores"",""http://www.nasdaq.com/symbol/flws"",";;
"FCTY,""1st Century Bancshares, Inc"",""4"",""36172000"",""n/a"",""n/a"",""Finance"",""Major Banks"",""http://www.nasdaq.com/symbol/fcty"",";;
"FCCY,""1st Constitution Bancorp (NJ)"",""8.8999"",""44908895.4"",""n/a"",""n/a"",""Finance"",""Savings Institutions"",""http://www.nasdaq.com/symbol/fccy"",";;
I'm trying to import Symbol, Sector, and Industry into a MySQL table with corresponding fields:
$path = "NASDAQ.csv";
$row = 1;
if (($handle = fopen($path, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$row++;
$entries[] = $data ;
}
fclose($handle);
}
foreach ($entries as $line) {
db_query("
INSERT INTO us_stocks (symbol, name, sector, industry)
VALUES ('%s', '%s', '%s', '%s', '%s')",
$line[0], $line[1], $line[6], $line[7]
);
}
The result, however, is not what I expected. In the database, only the Symbol field gets filled, and not even correctly:
symbol name sector industry
----------------------------------
Symbol,"Na
FLWS,"1-80
FCTY,"1st
FCCY,"1st
What am I doing wrong?
[edit]
If I print_r($entries), the output looks like
Array (
[0] => Array(
[0] => Symbol,"Name","LastSale","MarketCap","ADR TSO","IPOyear","Sector","industry","Summary Quote",;;
)
[1] => Array(
[0] => FLWS,"1-800 FLOWERS.COM, Inc.","2.9","81745200","n/a","1999","Consumer Services","Other Specialty Stores","http://www.nasdaq.com/symbol/flws",;;
)
[2] => Array(
[0] => FCTY,"1st Century Bancshares, Inc","4","36172000","n/a","n/a","Finance","Major Banks","http://www.nasdaq.com/symbol/fcty",;;
)
)
[edit2]
I have deleted the first line of the CSV, as suggested. I now have a very quick and dirty way of almost accomplishing what I want. Basically, the thing messes up whenever there's a company name with ", Inc" in it. So I just "glue" it to the name above: $data[1] = $data[1] . $data[2]:
$path = "NASDAQ.csv";
$row = 1;
if (($handle = fopen($path, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";;")) !== FALSE) {
if ($row < 100) {
$row++;
$data = explode(',', $data[0]);
if (substr($data[2], 0, 1) == ' ') {
$data[1] = $data[1] . $data[2];
unset($data[2]);
}
$entries[] = $data ;
}
}
fclose($handle);
}
A print_r($entries) now gives:
[0] => Array
(
[0] => FLWS
[1] => "1-800 FLOWERS.COM Inc."
[3] => "2.9"
[4] => "81745200"
[5] => "n/a"
[6] => "1999"
[7] => "Consumer Services"
[8] => "Other Specialty Stores"
[9] => "http://www.nasdaq.com/symbol/flws"
[10] =>
)
Final problem: I don't know how to renumber the keys. So 3 into 2, 4 into 3, etc. so that the output looks like:
[0] => Array
(
[0] => FLWS
[1] => "1-800 FLOWERS.COM Inc."
[2] => "2.9"
[3] => "81745200"
[4] => "n/a"
[5] => "1999"
[6] => "Consumer Services"
[7] => "Other Specialty Stores"
[8] => "http://www.nasdaq.com/symbol/flws"
[9] =>
)
Any help would be greatly appreciated!
I'd say the data isn't "truely" CSV.
"FLWS,""1-800 FLOWERS.COM, Inc."",""2.9"",
should be :
"FLWS","1-800 FLOWERS.COM, INC.","2.9" - The quotes should wrap the individual fields with commas seperating each field. Usually numeric fields are not wrapped.
Depending on how you load the data, comma's in the data may confuse it. (i.e. the FLOWERS.COM, INC"
By the way - if it's really CSV - look at: http://dev.mysql.com/doc/refman/5.1/en/load-data.html
As Crontab said, probably it's a problem with quotes. Try:
foreach ($entries as $line) {
// Escape (see mysql_real_escape_string too) and remove double quotes
foreach ($line as $k => $v) $line[$k] = mysql_escape_string(trim($v, '"'));
// Rebuild array
$line = array_values($line);
db_query("
INSERT INTO us_stocks (symbol, name, sector, industry)
VALUES ('%s', '%s', '%s', '%s', '%s')",
$line[0], $line[1], $line[6], $line[7]
);
}
PS: I don't know if you already escape strings in db_query().

Categories