I am able to receive the following JSON data with the following PHP code:
$json = file_get_contents('https://xxxx');
$data = json_decode($json,true);
$forex = $data['items'];
echo "<pre>";
print_r($forex);
exit;
This gives me the following JSON data:
Array
(
[0] => Array
(
[new] =>
[data] => Array
(
[direction] => 1
[pip] => 0
[exchange] => FOREX
[symbol] => USDCHF
[interval] => 15
[pattern] => Resistance
[complete] =>
[identified] => 2020-02-26T14:45:00.000Z
[age] => 0
[length] => 259
[found] => 2020-02-26T14:45:56.381Z
[result_type] => KeyLevel
[result_uid] => 642525551
[prediction_price_from] => 0
[prediction_price_to] => 0
[group_name] => FX Majors
[symbol_name] => USDCHF
[symbol_id] => 0
[analysis_text] => Approaching Resistance level of 0.9800 identified at 2/26 14:45. This pattern is still in the process of forming. Possible bullish price movement towards the resistance 0.9800 within the next 15 hours.
[expires_at] => 2020-02-27T05:48:36.701Z
)
[links] => Array
(
[0] => Array
(
[rel] => detail
[href] => https://xxxx
)
[1] => Array
(
[rel] => analysis
[href] => https://xxxx
)
[2] => Array
(
[rel] => chart-xs
[href] => https://xxxx
)
[3] => Array
(
[rel] => chart-sm
[href] => https://xxxx
)
[4] => Array
(
[rel] => chart-md
[href] => https://xxxx
)
[5] => Array
(
[rel] => chart-lg
[href] => https://xxxx
)
[6] => Array
(
[rel] => icon-arrow
[href] => https://xxxx
)
)
)
What I am trying to do is to save the information into my MySQL database, however I am unable to get the data displayed even by testing to echo the data:
foreach($forex as $info) {
echo $info->symbol . '<br>';
}
Anyone with a possible solution for me to save the data or even enable me to display the data for each Array (Example: symbol, pattern, link chart-md).
Below the commented code:
//Save json text into variable $json
$json = file_get_contents('https://xxxx');
//Convert json into PHP array
$data = json_decode($json,true);
//Set $forex as $data['items'];
$forex = $data['items'];
//Print
echo "<pre>";
print_r($forex);
exit;
$forex is a PHP array variable (not a JSON string).
You could save your data into MySQL VARCHAR[Length] field using json string variable $json instead of PHP array $forex.
When you need to get the data from DB and display it, you could get JSON data from MySQL and convert it from JSON to Array using PHP json_decode($json,true).
If you want to use a value into a PHP array, use $info["symbol"] instead of $info->symbol
Related
I have the following problem.
I am trying to read a Json file but I am not getting the desired result. I have the following data.
I would like to read the features and there is a mistake in finding out somewhere.
I get the error message when reading out:
Warning: Illegal string offset 'features'
Output
Array
(
[objectIdFieldName] => OBJECTID
[uniqueIdField] => Array
(
[name] => OBJECTID
[isSystemMaintained] => 1
)
[globalIdFieldName] =>
[geometryProperties] => Array
(
[shapeAreaFieldName] => Shape__Area
[shapeLengthFieldName] => Shape__Length
[units] => esriMeters
)
[geometryType] => esriGeometryPolygon
[spatialReference] => Array
(
[wkid] => 4326
[latestWkid] => 4326
)
[fields] => Array
(
[0] => Array
(
[name] => GEN
[type] => esriFieldTypeString
[alias] => GEN
[sqlType] => sqlTypeOther
[length] => 33
[domain] =>
[defaultValue] =>
)
[1] => Array
(
[name] => cases
[type] => esriFieldTypeInteger
[alias] => Anzahl Fälle
[sqlType] => sqlTypeOther
[domain] =>
[defaultValue] =>
)
[2] => Array
(
[name] => deaths
[type] => esriFieldTypeInteger
[alias] => Anzahl Todesfälle
[sqlType] => sqlTypeOther
[domain] =>
[defaultValue] =>
)
)
[features] => Array
(
[0] => Array
(
[attributes] => Array
(
[GEN] => Celle
[cases] => 220
[deaths] => 15
)
...........
My Code
$url = "https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=GEN%20%3D%20%27CELLE%27&outFields=GEN,cases,deaths&outSR=4326&f=json";
$json = file_get_contents($url);
$data = json_decode($json,true); //decode json result as array and thenloop it
print '<pre>';
print_r($data);
foreach($data as $row){
echo $row['features']->$row['attributes']->$row['GEN'];
}
Where am I wrong in reading?
It only has to be read what is in parentheses right?
So actually features-> attributes-> Gen to get the GEN query
Let's try change to this.
foreach($data as $row){
echo $row['features'][0]['attributes']['GEN'];
}
Sorry this is my fault when seem does'nt read clearly your question.
With GEN attribute you don't use loop to get this value.
If you wanna get GEN only your code should be that.
$url = "https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=GEN%20%3D%20%27CELLE%27&outFields=GEN,cases,deaths&outSR=4326&f=json";
$json = file_get_contents($url);
$data = (array) json_decode($json, true); //decode json result as array and thenloop it
print '<pre>';
print_r($data);
echo $data['features']['attributes']['GEN'];
// foreach($data as $row){
// echo $row['features']->$row['attributes']->$row['GEN'];
// }
Working on a personal project that will pull results from an API with full details of each pokemon.
So far I got the contents of the URL and returned the results into a JSON array format.
At the moment I am stuck on trying to retrieve results for[stats] inside from the array in an efficient manner.
private function getGenOnePokemon()
{
// the url of the api
$url = $this->baseUrl;
//get the contents of $url var and decode it into a json array
$json = file_get_contents($url , true);
$pokemon = json_decode($json, true, JSON_UNESCAPED_UNICODE);
// array output of pokemon
echo '<pre> ';
print_r($pokemon);
echo'</pre>';
//echo out value as speed
foreach($pokemon['results'][0] as $happy)
{
echo $happy['name'] . '<br />';
}
// echo base_stat value for speed with value of 90
echo $pokemon['stats'][0]['base_stat'];
}
However I do not seem to get anywhere much printing values/keys as I need to add something else to have full access to the values?
Would prefer not to directly access results, like I am doing with base_stat as plan on using this logic to pass into HTML View layer later.
Example of print_r dump (not full dump as really long) Full example: https://pokeapi.co/api/v2/pokemon/pikachu
Array
(
[forms] => Array
(
[0] => Array
(
[url] => https://pokeapi.co/api/v2/pokemon-form/25/
[name] => pikachu
)
)
[abilities] => Array
(
[0] => Array
(
[slot] => 3
[is_hidden] => 1
[ability] => Array
(
[url] => https://pokeapi.co/api/v2/ability/31/
[name] => lightning-rod
)
)
[1] => Array
(
[slot] => 1
[is_hidden] =>
[ability] => Array
(
[url] => https://pokeapi.co/api/v2/ability/9/
[name] => static
)
)
)
[stats] => Array
(
[0] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/6/
[name] => speed
)
[effort] => 2
[base_stat] => 90
)
[1] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/5/
[name] => special-defense
)
[effort] => 0
[base_stat] => 50
)
[2] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/4/
[name] => special-attack
)
[effort] => 0
[base_stat] => 50
)
[3] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/3/
[name] => defense
)
[effort] => 0
[base_stat] => 40
)
[4] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/2/
[name] => attack
)
[effort] => 0
[base_stat] => 55
)
[5] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/1/
[name] => hp
)
[effort] => 0
[base_stat] => 35
)
)
Any advice on how to access the data using foreach or other tips greatly appreciated. Thank you!
PHP has a specific function designed to target columnar data from arrays. It is called array_column()
If you want to isolate all of the name elements inside the forms subarray, use this:
$names=array_column($pokemon['forms'],'name');
If you want to isolate all of the base_stat elements inside of the stats subarray, use this:
$base_stats=array_column($pokemon['stats'],'base_stat');
Now you will have $names and $base_stats which are single-dimensional arrays by which you can perform additional processes or return from the function. Clean, intuitive, and simple.
Your $pokemon array doesn't contain a results field. There's only an forms field. So you should iterate over forms to print the names of the forms.
foreach($pokemon['forms'] as $happy) {
echo $happy['name'] . '<br />';
}
You could do the same thing with the stats
foreach($pokemon['stats'] as $stat) {
$base_stat = $stat['base_stat'];
// ...
}
I am trying to display the data from Bigcommerce through php when I run the Bigcommerce::getCategories() i getting a array of data like this:
[0] => Bigcommerce\Api\Resources\Category Object
(
[ignoreOnCreate:protected] => Array
(
[0] => id
[1] => parent_category_list
)
[ignoreOnUpdate:protected] => Array
(
[0] => id
[1] => parent_category_list
)
[fields:protected] => stdClass Object
(
[id] => 88
[parent_id] => 0
[name] => Dell
[description] =>
[sort_order] => 0
[page_title] =>
[meta_keywords] =>
[meta_description] =>
[layout_file] =>
[parent_category_list] => Array
(
[0] => 88
)
[image_file] =>
[is_visible] => 1
[search_keywords] =>
[url] => /dell/
)
[id:protected] => 88
[ignoreIfZero:protected] => Array
(
)
[fieldMap:protected] => Array
(
)
)
but when I try to pass this to JQuery so that I can display it using this statement: <?php echo json_encode($categories); ?> I am getting an array of empty objects, what is the right way to get the array of objects in Bigcommerce API? Thanks.
From the first line of your code:
[0] => Bigcommerce\Api\Resources\Category Object
You have an object, not an array. Try casting it to an array first:
$array = (array) $yourObject;
I'm trying to get data out of some JSON DATA. I'm using the following lines to decode it right now
$json_array = (array)(json_decode($response));
When I print my JSON Decoded array, I have the following data below:
I would like to get the details from the details section, where there is multiple sets of from/to_date's, and up/down numbers. Nothing I seem to do works though to get me to that data. I can print out the data from other areas like usage, but, I can't get into the details.
Array (
[resp_code] => SUCCESS
[caller_ref] => 2017092002282130006180
[server_ref] => 2017092002282169760291
[data] => stdClass Object (
[type] => monthly
[group_id] => 19
[device_id] => 32
[sn] => sn1234
[usages] => Array (
[0] => stdClass Object (
[from_date] => 2017-09-01T00:00:00
[to_date] => 2017-09-30T23:59:59
[up] => 22370
[down] => 119217
[ts] => 2017-09-01T00:00:00
)
)
[details] => stdClass Object (
[3] => Array (
[0] => stdClass Object (
[from_date] => 2017-09-01T00:00:00
[to_date] => 2017-09-30T23:59:59
[up] => 5522
[down] => 40301
[ts] => 2017-09-01T00:00:00
)
)
[2] => Array (
[0] => stdClass Object (
[from_date] => 2017-09-01T00:00:00
[to_date] => 2017-09-30T23:59:59
[up] => 6905
[down] => 32029
[ts] => 2017-09-01T00:00:00
)
)
)
)
)
Whats wrong with objects?
$obj = json_decode($response);
echo $obj->data->details[0]->from_date;
Or to loop it:
foreach ($obj->data->details as $item) {
echo $item->from_date;
// same goes for: to_date, up etc.
}
Simple and sexy!
Update:
It looks as if $item would be an array as well, so if you have problems try:
foreach ($obj->data->details as $item) {
echo $item[0]->from_date;
// same goes for: to_date, up etc.
}
I called JSON data and got following output:
stdClass Object
(
[request] => stdClass Object
(
[Target] => Affiliate_Report
[Format] => json
[Service] => HasOffers
[Version] => 3
[Method] => getConversions
[api_key] => my_key
[NetworkId] => icubes
[fields] => Array
(
[0] => Offer.name
[1] => Browser.display_name
[2] => Stat.payout
[3] => Stat.sale_amount
[4] => Stat.status
[5] => Stat.datetime
[6] => Stat.ip
[7] => Stat.ad_id
[8] => Stat.affiliate_info1
)
)
[response] => stdClass Object
(
[status] => 1
[httpStatus] => 200
[data] => stdClass Object
(
[page] => 1
[current] => 100
[count] => 75
[pageCount] => 1
[data] => Array
(
[0] => stdClass Object
(
[Offer] => stdClass Object
(
[name] => Myntra (CPS)
)
[Browser] => stdClass Object
(
[display_name] => Chrome
)
[Stat] => stdClass Object
(
[payout] => 150.00000
[sale_amount] => 954.00000
[status] => rejected
[datetime] => 2014-03-31 19:49:50
[ip] => 103.226.84.249
[ad_id] => 102ecc5fe230883c195d8a0e84ef7f
[affiliate_info1] =>
)
)
[1] => stdClass Object
(
[Offer] => stdClass Object
(
[name] => Myntra (CPS)
)
[Browser] => stdClass Object
(
[display_name] => Firefox
)
[Stat] => stdClass Object
(
[payout] => 270.00000
[sale_amount] => 545.00000
[status] => approved
[datetime] => 2014-04-18 12:00:20
[ip] => 27.0.51.119
[ad_id] => 10256740541d68b117955aa58529a6
[affiliate_info1] =>
)
)
I want to display that data in tabular form using table tag and also want to insert that data into a MySQL database.
But I dont understand the array format of data.
My code:
$result = file_get_contents($base);
$obj = json_decode($result);
echo"<table>";
foreach($obj as $item) {
echo"
<tr>
<td>$item['Offer.name']</td>
<td>$item['Browser.display_name']</td>
<td>$item['Stat.payout']</td>
<td>$item['Stat.sale_amount']</td>
<td>$item['Stat.datetime']</td>
<td>$item['Stat.ip']</td>
</tr>
";
}
echo"</table>";
This code gives me a blank output.
i used following code and got output but some errors are still there.
$result = file_get_contents($base);
$obj = json_decode($result, true);
echo"<table border=1>";
$i = 0;
foreach($obj['response'] as $item) {
for($i=0;$i<=10;$i++)
{
echo"
<tr>
<td>{$item['data'][$i]['Offer']['name']}</td>
<td>{$item['data'][$i]['Stat']['payout']}</td>
<td>{$item['data'][$i]['Stat']['sale_amount']}</td>
<td>{$item['data'][$i]['Stat']['status']}</td>
<td>{$item['data'][$i]['Stat']['datetime']}</td>
<td>{$item['data'][$i]['Stat']['ip']}</td>
</tr>
";
}
}
echo"</table>";
my output:
why upper rows are displaying blank values when there is no blank data is present?
and what are those errors?
You're using the wrong syntax. It's difficult to see exactly the results you're expecting, the values of the objects being returned look like they are objects of the original json themselves and without any useful data. However, to clarify for you:
You're returning the json_decode() results as an object and trying to access it as an array.
To return an associative array you need to add the 'assoc' parameter to the function, to read:
json_decode($result, true)
and you were trying to eco out the value directly from the associative array, you need to access it via the key, which will give you the value to read:
$result = file_get_contents($base);
$obj = json_decode($result, true);
echo"<table>";
foreach($obj['request'] as $item) {
echo"
<tr>
<td>{$item['fields'][0]}</td>
<td>{$item['fields'][1]}</td>
<td>{$item['fields'][2]}</td>
<td>{$item['fields'][3]}</td>
<td>{$item['fields'][4]}</td>
<td>{$item['fields'][5]}</td>
</tr>
";
}
echo"</table>";
Also, I've added the curly brackets inside the string to escape the variable expressions.
Since you're getting objects in your response, first try json_decode($result->request) and generally don't use [ ] as you normally would as if your result was array
EDIT: Try outputting the response like this:
echo '<pre>';
print_r($your-variable-here);
echo '</pre>';
Then let me know of the output