加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP CSV以特定方式呈现数组

发布时间:2020-12-13 21:44:17 所属栏目:PHP教程 来源:网络整理
导读:我知道fgetcsv,但它并没有真正做我想要的. 我有以下csv文件: productId,productName,productActive1,test product,12,test product2,0 我正在寻找能够创建如下所示数组的东西: array (0) ['productId'] = 1 ['productName'] = test product ['productActiv
我知道fgetcsv,但它并没有真正做我想要的.

我有以下csv文件:

productId,productName,productActive
1,test product,1
2,test product2,0

我正在寻找能够创建如下所示数组的东西:

array (0)
    ['productId'] => 1
    ['productName'] => test product
    ['productActive'] => 1

array (1)
    ['productId'] => 2
    ['productName'] => test product2
    ['productActive'] => 0

有任何想法吗?

解决方法

// open the file.
if (($handle = fopen("in.csv","r")) !== FALSE) {
        // read the column headers in an array.
        $head = fgetcsv($handle,1000,",");

        // read the actual data.
        while (($data = fgetcsv($handle,")) !== FALSE) {

                // create a new array with the elements in $head as keys
                // and elements in array $data as values.
                $combined = array_combine($head,$data);

                // print.
                var_dump($combined);
        }
        // done using the file..close it,fclose($handle);
}

See it

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读