Reputation: 1270
I have the following json after converting a remote csv file to JSON
convert code for csv to json
/////////////////////////////////////////////////////////
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Token ****',
'Accept-Version: ***',
));
$data = curl_exec($ch);
///////////////////////////////////////////////////////////////
if (curl_errno($ch)) {
print "Error: " . curl_error($ch);
} else {
// Show me the result
//var_dump($data);
curl_close($ch);
$array = array_map("str_getcsv", explode("\n", $data));
$encode = json_encode($array, true);
///////////////////////////////////////
It produces this:
[
"Timestamp",
"Point",
"Value",
"2016-03-11T14:40:00+00:00",
"ategho-leg_1-8",
"487.0",
"2016-03-11T14:40:00+00:00",
"ategho-leg_2-8",
"488.0",
"2016-03-11T14:40:00+00:00",
"ategho-leg_3-8",
"484.0",
"2016-03-11T14:40:00+00:00",
"ategho-temperature_long-8",
"466.0",
"2016-03-11T14:40:00+00:00",
"ategho-temperature_short-8",
"198.0",
"2016-03-11T14:45:00+00:00",
"ategho-leg_1-8",
"482.0",
"2016-03-11T14:45:00+00:00",
"ategho-leg_2-8",
"490.0",
"2016-03-11T14:45:00+00:00",
"ategho-leg_3-8",
"479.0",
"2016-03-11T14:45:00+00:00",
"ategho-temperature_long-8",
"464.0",
"2016-03-11T14:45:00+00:00",
"ategho-temperature_short-8",
"199.0",
null
]
And I want to turn it into data for use in morris charts, which wants the data in this format:
[
{"Timestamp":"2016-03-11T14:40:00+00:00", "Point":"ategho-leg_1-8", "Value":"487.0"}
]
I have searched stack and tried numersous things but I cannot get it to switch formats . I also tried stuff like this:
$new_final = array();
foreach($array as $value) {
foreach($value as $sub_value) {
$new_final[] = $sub_value;
}
}
echo json_encode($new_final);
Any help is appreciated. Thanks
EDIt: the first answer gives me code like this :
{
"Timestamp":[
"2016-03-11T14:40:00+00:00",
"ategho-leg_3-8",
"484.0"
],
"Point":[
"2016-03-11T14:40:00+00:00",
"ategho-temperature_long-8",
"466.0"
],
"Value":[
"2016-03-11T14:40:00+00:00",
"ategho-temperature_short-8",
"198.0"
]
},
Upvotes: 0
Views: 54
Reputation: 11689
The best way is to process directly data from csv without pre-encoding in JSON. BTW, this will works:
$data = json_decode( $json );
$result = array();
for( $i = 3; $i< count( $data ); $i=$i+3 )
{
$result[] = array
(
'Timestamp' => $data[$i],
'Point' => $data[$i+1],
'Value' => $data[$i+2]
);
}
echo json_encode( $result );
Basically, you need a for()` loop that start from 3rd item and that repeat each 3 items, then you can add to main array an associative array with your morris chart format.
In your example last JSON item is Null, so you have to decide if unset()
it or if unset()
last element of $result
before converting to JSON.
With modified question, you can process your array in this way:
$result = array();
for( $i = 1; $i < count( $array ); $i++ )
{
$result[] = array
(
'Timestamp' => $array[$i][0],
'Point' => $array[$i][1],
'Value' => $array[$i][2]
);
}
echo json_encode( $result );
Check before if the Null
issue persists.
Please note that for()
loop start from 1 to avoid header inclusion. If you doesn't have heads, you have to start loop form 0.
Upvotes: 1