从PHP远程CSV得到JSON数组 [英] get json array from remote csv in php

查看:230
本文介绍了从PHP远程CSV得到JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我的PHP code之一,我想JSON数组在PHP远程CSV文件以下。

远程CSV文件: www.xyz.com/dir/records.csv

 名字年龄ID
为Jhon 45 101
Bhil 42 102
音41 103

我想此CSV文件转换为JSON数组的函数

是这样的:

  $ JSON ='{记载:
       [
         {姓名:为Jhon,时代:45,ID:101},
         {姓名:Bhil,时代:42,ID:102},
         {姓名:音,时代:41,ID:103},
       ]
    };

请指引我,如何incopporate上述csv文件以获取该follwoing PHP code以上JSON阵列。

  $ myjson = json_de code($ JSON,真正的);
  的foreach($ myjson ['记录']为$行){
                        如果($行['身份证'] =='101'){
                          的foreach($行为$字段=> $值){
                               //做的工作在这里
                             }
                   }
         }


解决方案

下面是解决方案,您可以相应地修改输出数组里面的格式csvToJson()函数

 < PHP     功能csvToJson($文件名){
            $处理=的fopen($文件名,R);
            $ I = 0;
            如果($处理){
                而(($行= fgetcsv($处理,1000,,))!= = FALSE)
                {
                    如果($我== 0){
                        $ C = 0;
                        的foreach($线为$ COL){
                            $ COLS [$ C = $关口;
                            $ C ++;
                        }
                    }否则如果($ I&0){
                        $ C = 0;
                        的foreach($线为$ COL){
                            $数据[$ i] [$ COLS [$ C] = $关口;
                            $ C ++;
                        }
                    }
                    $ I ++;
                }
            }
            $ DATA2 ['记录'] =阵列($的数据);
            FCLOSE($处理);
            //返回json_en code($数据2); / *您不必将其转换成JSON,如果你想用它在你的PHP foreach循环可以直接返回此* /
            返回json_en code($数据2);
        }        $ JSON = csvToJson('records.csv');
        回声< pre>中;
        的print_r($ JSON);    ?>

For one of my php code, i want the json array as following from remote csv file in php.

The remote csv file: www.xyz.com/dir/records.csv

NAME    Age ID
Jhon    45  101
Bhil    42  102
Tone    41  103

I want a function which converts this CSV file to a JSON array

Something Like this:

$json = '{"records": 
       [
         {"Name":"Jhon", "Age":"45", "id":"101"},
         {"Name":"Bhil", "Age":"42", "id":"102"},
         {"Name":"Tone", "Age":"41", "id":"103"},
       ]
    }';

Kindly guide me, how to incopporate the above csv file to get the above json array for the follwoing php code.

$myjson = json_decode($json, true);  
  foreach ( $myjson['records'] as $row ) {  
                        if ($row['id'] =='101') { 
                          foreach ( $row as $field => $value ) {  
                               // do the work here
                             }
                   }
         }

解决方案

Here is the solution, You can modify your output array format accordingly inside csvToJson() function

<?php

     function csvToJson($filename) {
            $handle = fopen($filename,"r");
            $i=0;
            if($handle) {
                while(($line = fgetcsv($handle,1000,",","'")) !== FALSE)
                {
                    if($i == 0) {
                        $c = 0;
                        foreach($line as $col) {
                            $cols[$c] = $col;
                            $c++;
                        }
                    } else if($i > 0) {
                        $c = 0;
                        foreach($line as $col) {
                            $data[$i][$cols[$c]] = $col;
                            $c++;
                        }
                    }
                    $i++;
                }
            }
            $data2['records'] = array($data);       
            fclose($handle);
            // return json_encode($data2);  /*you don't have to convert it into json if you want to use it in your php foreach loop you can directly return this*/ 
            return json_encode($data2);
        }

        $json = csvToJson('records.csv');
        echo "<pre>";
        print_r($json);

    ?>

这篇关于从PHP远程CSV得到JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆