Reputation: 31
Good day everyone!
I have a CSV file, inside this:
1.453453,4.578413,0.314132,0.764312,0.123422 2.453453,3.578413,0.764312,0.314132,0.123422 3.453453,2.578413,0.123422,0.764312,0.314132 4.453453,1.578413,0.314132,0.123422,0.764312
To read this in postman this is my code:
$keys = ['PGA', 'PGV', 'X-Axis', 'Y-Axis', 'Z-Axis'];
$json = [];
$path = Storage::path('upload/test.txt');
$file = fopen($path, 'r');
while (($line = fgetcsv($file)) !== false) {
$json[] = array_combine($keys, $line);
}
fclose($file);
return json_encode($json);
The output is :
[
{
"PGA": "1.453453",
"PGV": "4.578413",
"X-Axis": "0.314132",
"Y-Axis": "0.764312",
"Z-Axis": "0.123422"
},
{
"PGA": "2.453453",
"PGV": "3.578413",
"X-Axis": "0.764312",
"Y-Axis": "0.314132",
"Z-Axis": "0.123422"
},
{
"PGA": "3.453453",
"PGV": "2.578413",
"X-Axis": "0.123422",
"Y-Axis": "0.764312",
"Z-Axis": "0.314132"
},
{
"PGA": "4.453453",
"PGV": "1.578413",
"X-Axis": "0.314132",
"Y-Axis": "0.123422",
"Z-Axis": "0.764312"
}
]
but I need to add 'data:' before the data and be inside another {}
:
like this:
{
"data": [
{
"PGA": "1.453453",
"PGV": "4.578413",
"X-Axis": "0.314132",
"Y-Axis": "0.764312",
"Z-Axis": "0.123422"
},
{
"PGA": "2.453453",
"PGV": "3.578413",
"X-Axis": "0.764312",
"Y-Axis": "0.314132",
"Z-Axis": "0.123422"
},
{
"PGA": "3.453453",
"PGV": "2.578413",
"X-Axis": "0.123422",
"Y-Axis": "0.764312",
"Z-Axis": "0.314132"
},
{
"PGA": "4.453453",
"PGV": "1.578413",
"X-Axis": "0.314132",
"Y-Axis": "0.123422",
"Z-Axis": "0.764312"
}
]
}
I am using angular as my front end and i need this to be shown. so i need to add the said above. thanks for every help you give! I have to put ' data: ' to call it inside the web page.
Thanks!
Upvotes: 0
Views: 241
Reputation: 350
$keys = ['PGA', 'PGV', 'X-Axis', 'Y-Axis', 'Z-Axis'];
$json = [];
$path = Storage::path('upload/test.txt');
$file = fopen($path, 'r');
while (($line = fgetcsv($file)) !== false) {
$json[] = array_combine($keys, $line);
}
fclose($file);
$modified = ["data" => $json];
return json_encode($modified);
Upvotes: 2