I need to convert a CSV file to JSON on the server using PHP. I am using this script which works:
function csvToJSON($csv) {
$rows = explode("\n", $csv);
$i = 0;
$len = count($rows);
$json = "{\n" . ' "data" : [';
foreach ($rows as $row) {
$cols = explode(',', $row);
$json .= "\n {\n";
$json .= ' "var0" : "' . $cols[0] . "\",\n";
$json .= ' "var1" : "' . $cols[1] . "\",\n";
$json .= ' "var2" : "' . $cols[2] . "\",\n";
$json .= ' "var3" : "' . $cols[3] . "\",\n";
$json .= ' "var4" : "' . $cols[4] . "\",\n";
$json .= ' "var5" : "' . $cols[5] . "\",\n";
$json .= ' "var6" : "' . $cols[6] . "\",\n";
$json .= ' "var7" : "' . $cols[7] . "\",\n";
$json .= ' "var8" : "' . $cols[8] . "\",\n";
$json .= ' "var9" : "' . $cols[9] . "\",\n";
$json .= ' "var10" : "' . $cols[10] . '"';
$json .= "\n }";
if ($i !== $len - 1) {
$json .= ',';
}
$i++;
}
$json .= "\n ]\n}";
return $json;
}
$json = csvToJSON($csv);
$json = preg_replace('/[ \n]/', '', $json);
header('Content-Type: text/plain');
header('Cache-Control: no-cache');
echo $json;
The $csv variable is a string resulting from a cURL request which returns the CSV content.
I am sure this is not the most efficient PHP code to do it because I am a beginner developer and my knowledge of PHP is low. Is there a better, more efficient way to convert CSV to JSON using PHP?
Thanks in advance.
Note. I am aware that I am adding whitespace and then removing it, I do this so I can have the option to return "readable" JSON by removing the line $json = preg_replace('/[ \n]/', '', $json); for testing purposes.
Edit. Thanks for your replies, based on them the new code is like this:
function csvToJson($csv) {
$rows = explode("\n", trim($csv));
$csvarr = array_map(function ($row) {
$keys = array('var0','var1','var2','var3','var4','var5','var6','var7','var8','var9','var10');
return array_combine($keys, str_getcsv($row));
}, $rows);
$json = json_encode($csvarr);
return $json;
}
$json = csvToJson($csv);
header('Content-Type: application/json');
header('Cache-Control: no-cache');
echo $json;
Well there is the json_encode() function, which you should use rather than building up the JSON output yourself. And there is also a function str_getcsv() for parsing CSV:
$array = array_map("str_getcsv", explode("\n", $csv));
print json_encode($array);
You must however adapt the $array if you want the JSON output to hold named fields.
I modified the answer in the question to use the first line of the CSV for the array keys. This has the advantage of not having to hard-code the keys in the function allowing it to work for any CSV with column headers and any number of columns.
Here is my modified version:
function csvToJson($csv) {
$rows = explode("\n", trim($csv));
$data = array_slice($rows, 1);
$keys = array_fill(0, count($data), $rows[0]);
$json = array_map(function ($row, $key) {
return array_combine(str_getcsv($key), str_getcsv($row));
}, $data, $keys);
return json_encode($json);
}
None of these answers work with multiline cells, because they all assume a row ends with '\n'. The builtin fgetcsv function understands that multiline cells are enclosed in " so it doesn't run into the same problem. The code below instead of relying on '\n' to find each row of a csv lets fgetcsv go row by row and prep our output.
function csv_to_json($file){
$columns = fgetcsv($file); // first lets get the keys.
$output = array(); // we will build out an array of arrays here.
while(!feof($file)){ // until we get to the end of file, we'll pull in a new line
$line = fgetcsv($file); // gets the next line
$lineObject = array(); // we build out each line with our $columns keys
foreach($columns as $key => $value){
$lineObject[$value] = $line[$key];
}
array_push($output, $lineObject);
}
return json_encode($output); // encode it as json before sending it back
}
Some tips...
If you have URL opening enabled for fopen() and wrappers, you can use fgetscsv().
You can build an array of the CSV, and then convert it with PHP's native json_encode().
The correct mime type for JSON is application/json.
You could probably reduce the overhead by removing all the spaces and \n's. But that's in your note.
You could increase the performance by skipping the preg_replace and passing a boolean that would turn it on and off.
Other than that, the variable unrolling of your var[1-10] actually is good, as long as there are always ten varaibles.
The explode and the foreach approach are just fine.
I recommend using Coseva (a csv parsing library) and using the built in toJSON() method.
<?php
// load
require('../src/CSV.php');
// read
$csv = new Coseva\CSV('path/to/my_csv.csv');
// parse
$csv->parse();
// disco
echo $csv->toJSON();
Related
I was recently tasked to update some older sites from MySQL to MySQLi.
Though slow and steady, the update has been ok until I ran into an issue when exporting some data to an excel document.
This code was written by a previous developer. There's a lot going on in the file, and I hope I'm grabbing the part that is supposed to be creating the excel document:
<?php
$export = mysqli_query ( $session->connDB(),$sql ) or die ( "Sql error : " . mysqli_error( ) );
$fields = mysqli_num_fields ( $export );
$num_rows = mysqli_num_rows( $export );
$pattern = '/[A-Z0-9][A-Z0-9._-]+#[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}/i'; //email
$phonpat = '/(\(?([0-9]{3})+\)?[\-\s\.]?([0-9]{3})+[\-\s\.]?([0-9]{4})(( |-|\.)?[ext\.]+ ?\d+)|\(?([0-9]{3})+\)?[\-\s\.]?([0-9]{3})+[\-\s\.]?([0-9]{4}))/i'; //telephone
$phPat = '/([0-9]{3})([0-9]{3})([0-9]{4})/';
$vippatt = '/VIP/i';
for($f=0; $f<$fields; $f++){
$header.='"'.mysqli_fetch_fields($export, $f).'"'."\t";
}
for($i=0; $i<$num_rows; $i++){
for($x=0; $x<$fields; $x++){
$email = mysqli_fetch_assoc($export,$i,"EMAIL");
$phone = mysqli_fetch_assoc($export,$i,"PHONE");
$viprm = mysqli_fetch_assoc($export,$i,"VIP");
preg_match ($pattern, $email, $matches);
preg_match ($phonpat, $phone, $phoneno);
preg_match ($vippatt, $viprm, $vpmatch);
if(isset($matches[0])) {$emal=strtolower($matches[0]);} else {$emal="";}
if(isset($vpmatch[0])) {$vips=strtoupper($vpmatch[0]);} else {$vips="";}
if(isset($phoneno[0])) {$phne=preg_replace($phPat,'($1) $2-$3 ',formatPhone($phoneno[0],false,false));} else {$phne="";}
if(mysqli_fetch_fields($export, $x)=='EMAIL'){
$fld=$emal;
} else {
if(mysqli_fetch_fields($export, $x)=='PHONE'){
$fld=$phne;
} else {
if(mysqli_fetch_fields($export, $x)=='VIP'){
$fld=$vips;
} else {
if(mysqli_fetch_fields($export, $x)=='UNITS'){
$fld=1;
} else {
$fld = mysqli_fetch_assoc($export,$i,mysqli_fetch_fields($export, $x));
}
}
}
}
$data.= '"'.$fld.'"'."\t";
}
$data.="\n";
}
?>
Here is where the code checks if the data is blank or not, and then exports the spreadsheet:
<?php
if ($data == "") {
$data = "\nNo records found for your search parameters.\n\n".$sql;
} else {
echo "should show data";
}
global $time;
$time = time();
header("Content-Disposition: attachment; filename=CargoManagementCustomReport-$time.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>
When the spreadsheet gets exported, I see "should show data". This tells me the $data variable obviously has data. It's just not getting into the spreadsheet.
If you'll notice in the above, I'm using mysqli_fetch_fields. This was used to replace mysql_field_name (in my attempt to update to MySQLi).
I also tried mysqli_fetch_field, but got the same results.
I am getting no errors, but the spreadsheet is still blank.
I can echo $sql to get the query, and I can run the query in the database and it returns data.
What am I doing wrong and how can I fix it?
That whole code is gibberish, so I hope I understood what it is that it was meant to do.
Here are the main problems:
mysqli_fetch_fields() takes only 1 argument and returns an array of objects. You can't cast an array to a string. I assume you wanted to get the field name.
mysqli_fetch_assoc() takes only 1 argument and returns an array of data in an associative array as the name suggests. It also moves the internal pointer to the next row every time it is called. You are trying to use it as if it was mysql_result().
Your nested loops are very messy. I replaced them with simple foreach loops and replaced the nested if statements with a switch. While I would normally stay away from such constructs, this is the easiest way to migrate this code.
After removing all the mysqli nonsense, the code is now readable. It iterates over every field of every row, applying some transformations to some fields and concatenating the result into a string.
Fixed code:
$conn = $session->connDB();
$export = mysqli_query($conn, $sql);
$pattern = '/[A-Z0-9][A-Z0-9._-]+#[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}/i'; //email
$phonpat = '/(\(?([0-9]{3})+\)?[\-\s\.]?([0-9]{3})+[\-\s\.]?([0-9]{4})(( |-|\.)?[ext\.]+ ?\d+)|\(?([0-9]{3})+\)?[\-\s\.]?([0-9]{3})+[\-\s\.]?([0-9]{4}))/i'; //telephone
$phPat = '/([0-9]{3})([0-9]{3})([0-9]{4})/';
$vippatt = '/VIP/i';
foreach (mysqli_fetch_fields($result) as $field) {
$header .= '"' . $field->name . '"' . "\t";
}
$data = '';
foreach ($export as $row) {
foreach ($rows as $fieldName => $value) {
switch ($fieldName) {
case 'EMAIL':
preg_match($pattern, $value, $matches);
$data .= '"' . (isset($matches[0]) ? strtolower($matches[0]) : '') . '"' . "\t";
break;
case 'PHONE':
preg_match($phonpat, $value, $phoneno);
$phne = "";
if (isset($phoneno[0])) {
$phne = preg_replace($phPat, '($1) $2-$3 ', formatPhone($phoneno[0], false, false));
}
$data .= '"' . $phne . '"' . "\t";
break;
case 'VIP':
preg_match($vippatt, $value, $vpmatch);
$data .= '"' . (isset($vpmatch[0]) ? strtolower($vpmatch[0]) : '') . '"' . "\t";
break;
case 'UNITS':
$data .= '"1"' . "\t";
break;
default:
$data .= '"' . $value . '"' . "\t";
break;
}
}
$data .= "\n";
}
I'm very new to PHP and JSON, so I hope you'll excuse my (probably) stupid question.
I'm trying to create a new JSON with fundamental information for my needings, using the information returned by Google Maps API.
Here's the code:
<?php
/**
* Created by PhpStorm.
* User: biagiomontesano
* Date: 22/10/15
* Time: 18:16
*/
function selectLongestSteps($steps_txt, $min_meters)
{
$steps = JSON.parse($steps_txt);
//$steps = json_decode($steps_txt, true);
//$steps = $steps_txt;
echo 'Num of steps: ' . count($steps[0]);
}
function createJsonFromResponse($response_json)
{
// output json
$output_json = '[';
// number of steps
$num_steps = count($response_json['routes'][0]['legs'][0]['steps']);
//echo $num_steps;
// fill the json
for($i = 0; $i<$num_steps; $i++)
{
// start parenthesis
$output_json .= '{';
// start latitude
$output_json .= '"start_lat":' . $response_json['routes'][0]['legs'][0]['steps'][$i]['start_location']['lat'] . ',';
// start longitude
$output_json .= '"start_lng":' . $response_json['routes'][0]['legs'][0]['steps'][$i]['start_location']['lng'] . ',';
// end latitude
$output_json .= '"end_lat":' . $response_json['routes'][0]['legs'][0]['steps'][$i]['end_location']['lat'] . ',';
// end latitude
$output_json .= '"end_lng":' . $response_json['routes'][0]['legs'][0]['steps'][$i]['end_location']['lng'] . ',';
// step length
$output_json .= '"step_length":' . $response_json['routes'][0]['legs'][0]['steps'][$i]['distance']['value']. ',';
// html instruction
$output_json .= '"instruction":"' . $response_json['routes'][0]['legs'][0]['steps'][$i]['html_instructions'] . '"';
// closure parenthesis
$output_json .= '}';
// insert comma if required
if($i != $num_steps-1)
$output_json .= ',';
}
$output_json .= ']';
return $output_json;
}
function get_driving_information($start, $finish)
{
if (strcmp($start, $finish) != 0) {
$start = urlencode($start);
$finish = urlencode($finish);
$url = 'http://maps.googleapis.com/maps/api/directions/json?origin=' . $start . '&destination=' . $finish . '&sensor=false';
// get the json response
$resp_json = file_get_contents($url);
// decode the json
$resp = json_decode($resp_json, true);
return $resp;
}
else
return null;
}
try
{
$info = get_driving_information('via Tiburtina 538, Roma', 'via Ariosto 25, Roma');
$steps = null;
if(!$info)
echo 'No info';
else
$steps = createJsonFromResponse($info);
selectLongestSteps($steps, 200);
//echo $steps;
}
catch(Exception $e)
{
echo 'Caught exception: '.$e->getMessage()."\n";
}
As a test, I asked my function selectLongestSteps to display the number of elements in the JSON.
As you see, I tried to initialise variable $steps in 3 different ways: the last two return a wrong results (0 or 1, while result should be 18).
The first one, that I found on web, returns the following error:
Notice: Use of undefined constant JSON - assumed 'JSON' in /Applications/XAMPP/xamppfiles/htdocs/prova_php/create_json.php on line 11
Fatal error: Call to undefined function parse() in /Applications/XAMPP/xamppfiles/htdocs/prova_php/create_json.php on line 11
I suspect the problem is in creating JSON inside the other function.
Can you help?
Thanks
The problem is this line:
$steps = JSON.parse($steps_txt);
JSON.parse() ist a JavaScript function. You need something like this in PHP
$steps = json_decode($steps_txt);
http://php.net/manual/en/function.json-decode.php
PHP has in-built function to change array to json and vice-verse.
json_encode($array) will give json form of array.
json_decode($json, true) will give your array back from json. If you didn't give true as your second param, you will be getting an object instead of an array.
{"status":"OK","data":{"train_number":"11111","chart_prepared":false,"pnr_number":"4259444444","train_name":"AAA","travel_date":{"timestamp":1394841600,"date":"10-4-2014"},"from":{"code":"ABC","name":"CITYJUNCTION","time":"20:30"},"to":{"code":"XYZ","name":"TTR","time":"05:35"},"alight":{"code":"DDD","name":"TTR","time":"05:35"},"board":{"code":"ABC","name":"FFF","time":"20:30","timestamp":1394895600},"class":"SL","passenger":[{"seat_number":"S7 , 11,GN","status":"CNF"},{"seat_number":"S7 , 06,GN","status":"CNF"}]}}
You'll need to use json_decode to convert the json string to an object or associative array, you may then iterate over the result. Remember to use htmlspecialchars or htmlentities to escape the data.
This should help get you started:
function table_encode(array $array) {
$buffer = '<table border="1"><thead>';
foreach (array_keys($array) as $header) {
$buffer .= '<th style="text-align:left">' . htmlspecialchars($header) . '</th>';
}
$buffer .= '</thead><tbody><tr>';
foreach ($array as $value) {
$buffer .= '<td>';
if (is_array($value)) {
$buffer .= table_encode($value);
} else {
$buffer .= htmlspecialchars($value);
}
$buffer .= '</td>';
}
$buffer .= '</tbody></table>';
return $buffer;
}
$json = '{"status":"OK","data":{"train_number":"11111","chart_prepared":false,"pnr_number":"4259444444","train_name":"AAA","travel_date":{"timestamp":1394841600,"date":"10-4-2014"},"from":{"code":"ABC","name":"CITYJUNCTION","time":"20:30"},"to":{"code":"XYZ","name":"TTR","time":"05:35"},"alight":{"code":"DDD","name":"TTR","time":"05:35"},"board":{"code":"ABC","name":"FFF","time":"20:30","timestamp":1394895600},"class":"SL","passenger":[{"seat_number":"S7 , 11,GN","status":"CNF"},{"seat_number":"S7 , 06,GN","status":"CNF"}]}}';
$json = json_decode($json, true);
$html = table_encode($json['data']);
echo $html;
$json = json_decode($array);//Decode Json
eccho($json);
function eccho($json){
foreach ($json as $file)
{
if(is_array($file))
return eccho($json);
else
echo "<tr>";
echo "<td> $file </td>";
echo "</tr>";
}
}
maybe this is useful for you...
you can't convert direct in to html..
use can use json_decode() function to convert it in to php array like follow or else use parse the json in jquery/javascript..
$var = '{"status":"OK","data":{"train_number":"11111","chart_prepared":false,"pnr_number":"4259444444","train_name":"AAA","travel_date":{"timestamp":1394841600,"date":"10-4-2014"},"from":{"code":"ABC","name":"CITYJUNCTION","time":"20:30"},"to":{"code":"XYZ","name":"TTR","time":"05:35"},"alight":{"code":"DDD","name":"TTR","time":"05:35"},"board":{"code":"ABC","name":"FFF","time":"20:30","timestamp":1394895600},"class":"SL","passenger":[{"seat_number":"S7 , 11,GN","status":"CNF"},{"seat_number":"S7 , 06,GN","status":"CNF"}]}}';
$ab = json_decode($var,true);
print_r($ab); // here u will get your json data in php array. so now you can print it in html based on your requirements..
hope it will help you
If your get response form server through AJAX then use
var json = '{"status":"OK","data":{"train_number":"11111","chart_prepared":false,"pnr_number":"4259444444","train_name":"AAA","travel_date":{"timestamp":1394841600,"date":"10-4-2014"},"from":{"code":"ABC","name":"CITYJUNCTION","time":"20:30"},"to":{"code":"XYZ","name":"TTR","time":"05:35"},"alight":{"code":"DDD","name":"TTR","time":"05:35"},"board":{"code":"ABC","name":"FFF","time":"20:30","timestamp":1394895600},"class":"SL","passenger":[{"seat_number":"S7 , 11,GN","status":"CNF"},{"seat_number":"S7 , 06,GN","status":"CNF"}]}}';
obj = JSON.parse(json);
or use
data = eval( '(' + json + ')');
Now you can access
var status = data.status;
and you can easily display display data in html.
I need to update a file using php
Sample file:
#Start#
No. of records: 2
Name: My name,
Age: 18,
Date: 2013-07-11||
Name: 2nd name,
Age: 28,
Date: 2013-07-11||
#End#
I need to edit 'No. of records' on each time I add another record on file. And another record needs to be before '#End#'
I'm using
$Handle = fopen($File, 'a');
$data = .......
fwrite($Handle, $Data);
to add records
How can I edit 'No. of records' & add data before '#End#'?
Instead of modifying the file I would parse it, change the data in PHP an rewrite the file after that.
To achieve this, I would firstly create a function that parses the input into php arrays:
function parse($file) {
$records = array();
foreach(file($file) as $line) {
if(preg_match('~^Name: (.*),~', $line, $matches)) {
$record = array('name' => $matches[1]);
}
if(preg_match('~^Age: (.*),~', $line, $matches)) {
$record ['age'] = $matches[1];
}
if(preg_match('~^Date: (.*)\|\|~', $line, $matches)) {
$record ['date'] = $matches[1];
$records [] = $record;
}
}
return $records;
}
Secondly I would create a function that flattens the arrays back into the same file format again:
function flatten($records, $file) {
$str = '#Start#';
$str .= "\n\n";
$str .= 'No. of records: ' . count($records) . "\n\n";
foreach($records as $record) {
$str .= 'Name: ' . $record['name'] . ",\n";
$str .= 'Age: ' . $record['name'] . ",\n";
$str .= 'Date: ' . $record['name'] . "||\n\n";
}
file_put_contents($file, $str . '#End#');
}
Then use it like this:
$records = parse('your.file');
var_dump($records);
$records []= array(
'name' => 'hek2mgl',
'age' => '36',
'date' => '07/11/2013'
);
flatten($records, 'your.file');
In case if file is relatively small (easily fits in memory), you can use file() function. It will return array, which you can iterate, etc.
If the file is larger, you'll need to read it in the loop using fgets(), writing data to the new temporary file and replacing original file with it after you're done
How to get all pid and styles attribute from following json data with minimum loop in php
{"general":{"note":{"display":false}},"elements":{"the-1":{"index":1,"src":"shirt1.png","pid":"pid-3563130","angle":0,"styles":"background:transparent;top:51.80000305175781px;left:122px;width:80px;height:80px;","background":"transparent","pos":{"top":51.80000305175781,"left":122},"size":{"width":80,"height":80},"details":{"other":""}},"the-2":{"index":2,"src":"shirt2.png","pid":"pid-132002","angle":0,"styles":"background:transparent;top:44.80000305175781px;left:155px;width:80px;height:80px;","background":"transparent","pos":{"top":44.80000305175781,"left":155},"size":{"width":80,"height":80},"details":{"other":""}}}}
Thanks
$str = '{"general":{"note":{"display":false}},"elements":{"the-1":{"index":1,"src":"shirt1.png","pid":"pid-3563130","angle":0,"styles":"background:transparent;top:51.80000305175781px;left:122px;width:80px;height:80px;","background":"transparent","pos":{"top":51.80000305175781,"left":122},"size":{"width":80,"height":80},"details":{"other":""}},"the-2":{"index":2,"src":"shirt2.png","pid":"pid-132002","angle":0,"styles":"background:transparent;top:44.80000305175781px;left:155px;width:80px;height:80px;","background":"transparent","pos":{"top":44.80000305175781,"left":155},"size":{"width":80,"height":80},"details":{"other":""}}}}';
$arr = json_decode($str, true);
foreach ($arr['elements'] as $element) {
echo 'pid: ' . $element['pid'] . '<br />';
echo 'styles: ' . $element['styles'] . '<br />';
}
use json_decode function in PHP to get assosiative array.
<?php
$myJson = '{"general":{"note":{"display":false}},"elements":{"the-1":{"index":1,"src":"shirt1.png","pid":"pid-3563130","angle":0,"styles":"background:transparent;top:51.80000305175781px;left:122px;width:80px;height:80px;","background":"transparent","pos":{"top":51.80000305175781,"left":122},"size":{"width":80,"height":80},"details":{"other":""}},"the-2":{"index":2,"src":"shirt2.png","pid":"pid-132002","angle":0,"styles":"background:transparent;top:44.80000305175781px;left:155px;width:80px;height:80px;","background":"transparent","pos":{"top":44.80000305175781,"left":155},"size":{"width":80,"height":80},"details":{"other":""}}}}';
$myArray = json_decode($myJson,true);
$myInnerArray = $myArray['elements'];
$styles = array();
foreach($myInnerArray as $element)
$styles[] = $element['styles'];
print_r($styles);
?>
PHP has great abilities to handle json.
Let's assume the JSON string you've posted above is stored in a PHP variable $myJSON.
So we can easily store an associative array of these values into $myJSONArray like so:
$myJSONArray = json_decode( $myJSON, true );
So, now we just loop through:
foreach( $myJSONArray['elements'] as $arr => $key )
echo( "A PID: " . $key['pid'] . "\n" );
See it in action on Codepad.
$json = json_decode('{"general":{"note":{"display":false}},"elements":{"the-1":{"index":1,"src":"shirt1.png","pid":"pid-3563130","angle":0,"styles":"background:transparent;top:51.80000305175781px;left:122px;width:80px;height:80px;","background":"transparent","pos":{"top":51.80000305175781,"left":122},"size":{"width":80,"height":80},"details":{"other":""}},"the-2":{"index":2,"src":"shirt2.png","pid":"pid-132002","angle":0,"styles":"background:transparent;top:44.80000305175781px;left:155px;width:80px;height:80px;","background":"transparent","pos":{"top":44.80000305175781,"left":155},"size":{"width":80,"height":80},"details":{"other":""}}}}', true);
$elements = $json['elements'];
foreach($elements as $element){
$pid = $element['pid'];
$styles = $element['styles'];
echo $pid.': '.$styles.'<br />';
}
Example here