Thor
Honorary Master
- Joined
- Jun 5, 2014
- Messages
- 44,236
Yes.
What did you think it was?
Not a single string, obviously.
Yes.
What did you think it was?
Yes.
What did you think it was?
<?php
$input = '[
{
"dateTime": "2016-08-10T21:00:00Z",
"value": 13.2946
},
{
"dateTime": "2016-08-10T21:05:00Z",
"value": 13.2947
},
{
"dateTime": "2016-08-10T21:10:00Z",
"value": 13.2959
},
{
"dateTime": "2016-08-10T21:15:00Z",
"value": 13.2989
},
{
"dateTime": "2016-08-10T23:00:00Z",
"value": 13.3006
}
]';
function convert($v)
{
return [strtotime($v->dateTime), $v->value];
}
$text = json_decode($input);
date_default_timezone_set("UTC");
$encoded = json_encode(array_map('convert', $text));
print($encoded);
?>
[[1470862800,13.2946],[1470863100,13.2947],[1470863400,13.2959],[1470863700,13.2989],[1470870000,13.3006]]
[)roi(];18131303 said:PHP:<?php $input = '[ { "dateTime": "2016-08-10T21:00:00Z", "value": 13.2946 }, { "dateTime": "2016-08-10T21:05:00Z", "value": 13.2947 }, { "dateTime": "2016-08-10T21:10:00Z", "value": 13.2959 }, { "dateTime": "2016-08-10T21:15:00Z", "value": 13.2989 }, { "dateTime": "2016-08-10T23:00:00Z", "value": 13.3006 } ]'; function convert($v) { return [strtotime($v->dateTime), $v->value]; } $text = json_decode($input); date_default_timezone_set("UTC"); $encoded = json_encode(array_map('convert', $text)); print($encoded); ?>
[)roi(];18131321 said:The only bit I saw was a partial copy; I'll help if you link to the full JSON.
[)roi(];18131321 said:The only bit I saw was a partial copy; I'll help if you link to the full JSON.
$arr = json_decode($jsonString, true);
<?php
$input = file_get_contents("http://www.bloomberg.com/markets/api/quote-page/usdzar:CUR");
function convert($v)
{
return [strtotime($v->dateTime), $v->value];
}
$text = json_decode($input);
date_default_timezone_set("UTC");
$encoded = json_encode(array_map('convert', $text->priceTimeSeries[0]->price));
print($encoded);
?>
I'm just skimming so this may be totally OT... you have a json string and you want an array in php?
Code:$arr = json_decode($jsonString, true);
I still can't get the price array I keep getting non object error
It's the structure and hierarchy that I'm not getting it would seem ie I don't know how to loop it to get the values of pricetimeseries -> price.
$bloomberg_array = json_decode($bloomberg_json, true);
foreach($bloomberg_array['priceTimeSeries']['price'] as $price) {
echo $price['dateTime'] . '<br>';
echo $price['value'] . '<br>';
}
No.
I take some values from that JSON but I also require and array of the price time
That JSON is a bunch of nested arrays after a very specific array inside the array if that makes sense.
$arr = json_decode($jsonString, true);
$prices = $arr['priceTimeSeries'][0]['price'];
[)roi(];18131439 said:PHP:<?php $input = file_get_contents("http://www.bloomberg.com/markets/api/quote-page/usdzar:CUR"); function convert($v) { return [strtotime($v->dateTime), $v->value]; } $text = json_decode($input); date_default_timezone_set("UTC"); $encoded = json_encode(array_map('convert', $text->priceTimeSeries[0]->price)); print($encoded); ?>
As to storing the data in MYSQL; you have two options; store each time/price value as individual records; array_map & join can help you to easily construct the SQL insert command.
Alternatively store the entire unencoded string value in a column (a large one); in the above php code; this would be the $input variable.
//Currency Updates
// set quotes
$quotes = array("USDZAR", "EURZAR", "GBPZAR");
//Parse about any English textual datetime description into a Unix timestamp
function convert($v)
{
return [strtotime($v->dateTime), $v->value];
}
//Start the loop
foreach ($quotes as $quote) {
$url = "http://www.bloomberg.com/markets/api/quote-page/$quote:CUR";
$json = file_get_contents($url);
$json = json_decode($json);
$currency_name = $json->basicQuote->securityName;
$currency_price = $json->basicQuote->price;
$currency_percentage = round($percentage = $json->basicQuote->percentChange1Day, 2, PHP_ROUND_HALF_UP);
$currency_history = json_encode(array_map('convert', $json->priceTimeSeries[0]->price));
$modified_time = date("Y-m-d H:i:s");
echo "Currency Name $currency_name";
echo "<br>";
echo "Currency Price $currency_price";
echo "<br>";
echo "Currency Change $currency_percentage";
echo "<br>";
echo $currency_history;
echo "<br>";
}

No worries; simple conversion of _kabal_'s approach to use of higher order functions.I LOVE YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!