0

I have

Array
(
    [Name] => sdvsdv
    [CanaryAMS__Account_id__c] => abc
    [CanaryAMS__S3_File_Url__c] => ascfdsv
    [CanaryAMS__Description__c] => statement faxed to Aurelia at Occi 9/29/09
    [CanaryAMS__DocExternalId__c] => dscmdsv
)

i am using following code but it is giving error

<?php

$list = Array
(
    Name => sdvsdv
    CanaryAMS__Account_id__c => abc
    CanaryAMS__S3_File_Url__c => ascfdsv
    CanaryAMS__Description__c => statement faxed to Aurelia at Occi 9/29/09
    CanaryAMS__DocExternalId__c => dscmdsv
);

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);
?>

I want to upload this data into csv file. sdvsdv under name , abc under CanaryAMS__Account_id__c and so on.

1 Answer 1

1

try this. There are probable better ways of doing it though but for this simple example there is no need.

<?php

$list[0] = array
(
    'Name' => 'sdvsdv',
    'CanaryAMS__Account_id__c' => 'abc',
    'CanaryAMS__S3_File_Url__c' => 'ascfdsv',
    'CanaryAMS__Description__c' => 'statement faxed to Aurelia at Occi 9/29/09',
    'CanaryAMS__DocExternalId__c' => 'dscmdsv'
);
$list[1] = array
(
    'Name' => 'khk',
    'CanaryAMS__Account_id__c' => 'abckj',
    'CanaryAMS__S3_File_Url__c' => 'ghkghkv',
    'CanaryAMS__Description__c' => 'statement faxed to Aurelia at Occi 9/29/15',
    'CanaryAMS__DocExternalId__c' => 'dscmdsv'
);
$list[2] = array
(
    'Name' => 'gghfhfghf',
    'CanaryAMS__Account_id__c' => 'affbc',
    'CanaryAMS__S3_File_Url__c' => 'ascfffdsv',
    'CanaryAMS__Description__c' => 'statement faxed to Aurelia at Occi 05/05/20',
    'CanaryAMS__DocExternalId__c' => 'dscmdsv'
);

$header = '';
$fp = fopen('file.csv', 'w');
foreach ($list[0] as $k=>$v) $header .= $k.",";
$csvstring = trim($header,",")."\n";
fwrite($fp,$csvstring);
foreach ($list as $fields) fputcsv($fp, $fields);
fclose($fp);
Sign up to request clarification or add additional context in comments.

5 Comments

I can do same thing for array which contains 20 records right?array[0],array[1] etc
sure - u can have as many as your system will allow you to have limited by memory and your php ini settings (some of which may or may not be able to overridden in the php file itself)
My array is in the form Array ( [Name] => sdvsdv [CanaryAMS__Account_id__c] => abc [CanaryAMS__S3_File_Url__c] => ascfdsv [CanaryAMS__Description__c] => statement faxed to Aurelia at Occi 9/29/09 [CanaryAMS__DocExternalId__c] => dscmdsv ) This array is generated from some code actually
if you have multiple arrays only one level deep then you will need to combine them into a nested array like i have sampled above otherwise you will need to call the same functions over and over again with anything other than the first array excluding the header part and of course opening the file with the 'a' flag and not the 'w' flag so not to truncate the file
Please help me with above mentioned array Array ( [Name] => sdvsdv [CanaryAMS__Account_id__c] => abc [CanaryAMS__S3_File_Url__c] => ascfdsv [CanaryAMS__Description__c] => statement faxed to Aurelia at Occi 9/29/09 [CanaryAMS__DocExternalId__c] => dscmdsv )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.