Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have several method to transform php array to csv string both from stackoverflow and google. But I am in trouble that if I want to store mobile number such as 01727499452, it saves as without first 0 value. I am currently using this piece of code: Convert array into csv

Can anyone please help me.

Array   

[1] => Array
    (
        [0] => Lalu ' " ; \\ Kumar
        [1] => Mondal
        [2] => 01934298345
        [3] => 
    )

[2] => Array
    (
        [0] => Pritom
        [1] => Kumar Mondal
        [2] => 01727499452
        [3] => Bit Mascot
    )

[3] => Array
    (
        [0] => Pritom
        [1] => Kumar Mondal
        [2] => 01711511149
        [3] => 
    )

[4] => Array
    (
        [0] => Raaz
        [1] => Mukherzee
        [2] => 01911224589
        [3] => Khulna University 06
    )

)

My code block:

function arrayToCsv( array $fields, $delimiter = ';', $enclosure = '"', $encloseAll = false, $nullToMysqlNull = false ) {
$delimiter_esc = preg_quote($delimiter, '/');
$enclosure_esc = preg_quote($enclosure, '/');

$outputString = "";
foreach($fields as $tempFields) {
    $output = array();
    foreach ( $tempFields as $field ) {
        if ($field === null && $nullToMysqlNull) {
            $output[] = 'NULL';
            continue;
        }

        // Enclose fields containing $delimiter, $enclosure or whitespace
        if ( $encloseAll || preg_match( "/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field ) ) {
            $field = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure;
        }
        $output[] = $field." ";
    }
    $outputString .= implode( $delimiter, $output )."\r\n";
}
return $outputString; }

Thanks,

Pritom.

share|improve this question
1  
That's the problem with using other people's wheels. – Steve May 3 at 6:03
1  
Save it as a string. I'd recommend that for phone numbers anyway, because you're just going to want to save that + as well ;) – SBI May 3 at 6:04
Can you give an example of how your array looks like? – bestprogrammerintheworld May 3 at 6:06
2  
You should be using PHP's built in CSV functions like php.net/manual/en/function.fputcsv.php – cryptic ツ May 3 at 6:07
Some second-hand advice: If you're not going to do arithmetic on it, store it as a string. The Daily WTF contains examples of many a phone number printed in scientific notation, which ultimately stems from someone storing it as an integer. – michaelb958 May 3 at 6:11
show 2 more commentsadd comment (requires an account with 50 reputation)

3 Answers

Is this what you need?

$out = "";
foreach($array as $arr) {
    $out .= implode(",", $arr) . "\r\n";

}

It runs trough your array creating a new line on each loop seperating the array values with a ",".

share|improve this answer
Is this what I want? Please read my question and if you are not clear please ask me. – Pritom May 3 at 6:29
It should be, i dont see why you wouldnt:) Give it a shot, and see if it meets your expectations. – Daniel Mensing May 3 at 6:29
No valid result – Pritom May 3 at 6:32
add comment (requires an account with 50 reputation)

Are you sure the numbers are actually being saved without the leading zero? Have you looked at the actual CSV output in a text editor?

If you've just opened up the CSV file in a spreadsheet application, it is most likely the spreadsheet that is interpreting your telephone numbers as numeric values and dropping the zeros when displaying them. You can usually fix that in the spreadsheet by changing the formatting options on that particular column.

share|improve this answer
add comment (requires an account with 50 reputation)

You could use str_putcsv function:

if(!function_exists('str_putcsv'))
{
    function str_putcsv($input, $delimiter = ',', $enclosure = '"')
    {
        // Open a memory "file" for read/write...
        $fp = fopen('php://temp', 'r+');
        // ... write the $input array to the "file" using fputcsv()...
        fputcsv($fp, $input, $delimiter, $enclosure);
        // ... rewind the "file" so we can read what we just wrote...
        rewind($fp);
        // ... read the entire line into a variable...
        $data = fread($fp, 1048576);
        // ... close the "file"...
        fclose($fp);
        // ... and return the $data to the caller, with the trailing newline from fgets() removed.
        return rtrim($data, "\n");
    }
 }

 $csvString = '';
 foreach ($list as $fields) {
     csvString .= str_putcsv($fp, $fields);
 }

More about this on GitHub, a function created by @johanmeiring.

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.