在php中从远程csv获取json数组
对于我的一段php代码,我希望从php中的远程csv文件获得如下json数组 远程csv文件:www.xyz.com/dir/records.csv在php中从远程csv获取json数组,php,arrays,json,csv,Php,Arrays,Json,Csv,对于我的一段php代码,我希望从php中的远程csv文件获得如下json数组 远程csv文件:www.xyz.com/dir/records.csv NAME Age ID Jhon 45 101 Bhil 42 102 Tone 41 103 我想要一个将这个CSV文件转换成JSON数组的函数 大概是这样的: $json = '{"records": [ {"Name":"Jhon", "Age":"45", "id":"101
NAME Age ID
Jhon 45 101
Bhil 42 102
Tone 41 103
我想要一个将这个CSV文件转换成JSON数组的函数
大概是这样的:
$json = '{"records":
[
{"Name":"Jhon", "Age":"45", "id":"101"},
{"Name":"Bhil", "Age":"42", "id":"102"},
{"Name":"Tone", "Age":"41", "id":"103"},
]
}';
请指导我,如何对上面的csv文件进行压缩,以获得下面php代码的上面json数组
$myjson = json_decode($json, true);
foreach ( $myjson['records'] as $row ) {
if ($row['id'] =='101') {
foreach ( $row as $field => $value ) {
// do the work here
}
}
}
这里是解决方案,您可以在csvToJson()函数中相应地修改输出数组格式
如果csv存储在本地或主机上支持远程地址的fopen中,则可以编写与此类似的函数:
function csvToJson($filename) {
$content = file_get_contents('www.xyz.com/dir/records.csv');
// $content = file_get_contents('https://docs.shopify.com/manual/your-store/products/product_template.csv'); /* demo URL *-/
$handle = fopen($filename,"w");
fwrite($handle, $content);
fclose($handle);
$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) {
if($col != ''){
$data[$i][$cols[$c]] = $col;
}
$c++;
}
}
$i++;
}
}
$data2['records'] = array($data);
fclose($handle);
return json_encode($data2);
}
$json = csvToJson('records.csv');
echo "<pre>";
print_r($json);
如果您必须从远程服务器访问csv文件,您可以使用此选项,也可以使用cURL在服务器上保存文件,然后使用csvToJson()函数(如果您在服务器上使用cURL下载文件,请注释函数的前四行)
函数csvToJson($filename){
$content=file_get_contents('www.xyz.com/dir/records.csv');
//$content=file\u get\u contents('https://docs.shopify.com/manual/your-store/products/product_template.csv');/*演示URL*-/
$handle=fopen($filename,“w”);
fwrite($handle,$content);
fclose($handle);
$handle=fopen($filename,“r”);
$i=0;
如果($handle){
while(($line=fgetcsv($handle,1000,“,”,“”))!==FALSE)
{
如果($i==0){
$c=0;
foreach($col行){
$cols[$c]=$col;
$c++;
}
}否则如果($i>0){
$c=0;
foreach($col行){
如果($col!=''){
$data[$i][$cols[$c]]=$col;
}
$c++;
}
}
$i++;
}
}
$data2['records']=数组($data);
fclose($handle);
返回json_encode($data2);
}
$json=csvToJson('records.csv');
回声“;
打印(json);
您想创建类似$json的数组,还是想将它们放入csv?要从远程csv文件创建类似$json的json数组
<?php
//pass the filename and optional the separator used in your csv to this function
function csvToJson($filename, $separator = ";")
{
//create the resulting array
$result = array("records" => array());
//check if the file handle is valid
if (($handle = fopen($filename, "r")) !== false)
{
//check if the provided file has the right format
if(($data = fgetcsv($handle, 4096, $separator)) == false || ($data[0] != "NAME" || $data[1] != "Age" || $data[2] != "ID"))
{
throw new InvalidImportFileFormatException(sprintf('The provided file (%s) has the wrong format!', $filename));
}
//loop through your data
while (($data = fgetcsv($handle, 4096, $separator)) !== false)
{
//store each line in the resulting array
$result['records'][] = array("Age" => $data[0], "Name" => $data[1], "Id" => $data[2]);
}
//close the filehandle
fclose($handle);
}
//return the json encoded result
return json_encode($result);
}
function csvToJson($filename) {
$content = file_get_contents('www.xyz.com/dir/records.csv');
// $content = file_get_contents('https://docs.shopify.com/manual/your-store/products/product_template.csv'); /* demo URL *-/
$handle = fopen($filename,"w");
fwrite($handle, $content);
fclose($handle);
$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) {
if($col != ''){
$data[$i][$cols[$c]] = $col;
}
$c++;
}
}
$i++;
}
}
$data2['records'] = array($data);
fclose($handle);
return json_encode($data2);
}
$json = csvToJson('records.csv');
echo "<pre>";
print_r($json);