Convert php array to csv string

后端 未结 8 1359
一向
一向 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:12

    Inspired by @alexcristea's answer:

    function array2csv($data, $delimiter = ',', $enclosure = '"', $escape_char = "\\")
    {
        $f = fopen('php://memory', 'r+');
        foreach ($data as $item) {
            fputcsv($f, $item, $delimiter, $enclosure, $escape_char);
        }
        rewind($f);
        return stream_get_contents($f);
    }
    
    $list = array (
        array('aaa', 'bbb', 'ccc', 'ffffdd'),
        array('123', '456', '789'),
        array('"aaa"', '"bbb"')
    );
    var_dump(array2csv($list));
    

提交回复
热议问题