Convert php array to csv string

后端 未结 8 1279
一向
一向 2020-12-10 02:47

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,

相关标签:
8条回答
  • 2020-12-10 03:19

    The function above is not exactly right cause it considers \n like an element, which is not what we want as each line must be only separated by \n. So a more efficient code would be:

    function array2csv($array, $delimiter = "\n") {
        $csv = array();
        foreach ($array as $item=>$val) 
        {
            if (is_array($val)) 
            { 
                $csv[] = $this->array2csv($val, ";");
            } 
            else 
            {
                $csv[] = $val;
            }
        }
        return implode($delimiter, $csv);
    }
    
    0 讨论(0)
  • 2020-12-10 03:26

    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.

    0 讨论(0)
提交回复
热议问题