How do I get variable values from this JSON array? - php

So I have a URL that returns the following:
[{"_id":{"champ2_id":63,"champ1_id":2,"role":"TOP"},"count":4,"champ1":{"thirtyToEnd":0,"goldEarned":10727.5,"zeroToTen":0,"minionsKilled":158,"winrate":0,"assists":6.25,"role":"TOP","deaths":6,"kills":4,"wins":0,"totalDamageDealtToChampions":17350.75,"twentyToThirty":0,"tenToTwenty":0,"neutralMinionsKilledTeamJungle":1.75,"killingSprees":0.75,"weighedScore":27214.5375},"champ2":{"twentyToThirty":0,"wins":4,"winrate":1,"kills":5.75,"neutralMinionsKilledTeamJungle":5,"totalDamageDealtToChampions":21881.25,"role":"TOP","assists":7,"tenToTwenty":0,"thirtyToEnd":0,"zeroToTen":0,"goldEarned":12371.75,"killingSprees":1.25,"minionsKilled":140.5,"deaths":4.25,"weighedScore":33166.587499999994}]
I have learned how to get the value of a key in an array when the URL returns something simpler. For example if the URL returns:
{"id":34743514,"accountId":49161997,"name":"League of Fiddle","profileIconId":786,"revisionDate":1514093712000,"summonerLevel":52}
I can echo the id with this code:
$json = file_get_contents(URL);
$data = json_decode($json, true);
echo $data['id'];
That's easy. But when I try to use the same code for the more complicated stuff, like say I want to get the value of _id champ2_id, I've tried:
$json = file_get_contents(URL);
$data = json_decode($json, true);
echo $data['_id']['champ2_id'];
But this says _id is an undefined index. What am I doing wrong?

should be
$data[0]['_id']['champ2_id'];

Related

How to read specific data from an object?

{"TransactionInfo":{"Date":"06\/04\/2018","Time":"09:29 ET","TransactionCharge":{"MonetaryValue":"0.00","CurrencyCode":{}}},"ShipmentEstimate":{"CurrencyCode":"EUR","ShipmentCharges":{"TaxesAndFees":"1.7532","AdditionalInsuranceCost":"0.00","TransportationCost":"0.00","SubTotal":"1.7532"},"ProductsCharges":{"Product":{"TariffCode":"5109.90.80.00","Charges":{"Duties":"75.0021","TaxesAndFees":"0.00","VAT":"0.00","CostOfGoods":"1250.00","SubTotal":"1325.0021"}},"ProductsSubTotal":"1325.0021"},"TotalLandedCost":"1326.7553"},"SuppressQuestionIndicator":"Y"}
How can I get only the value of TariffCode?
Result must be: 5109.90.80.00
<?php
$json = '{"TransactionInfo":{"Date":"06\/04\/2018","Time":"09:29 ET","TransactionCharge":{"MonetaryValue":"0.00","CurrencyCode":{}}},"ShipmentEstimate":{"CurrencyCode":"EUR","ShipmentCharges":{"TaxesAndFees":"1.7532","AdditionalInsuranceCost":"0.00","TransportationCost":"0.00","SubTotal":"1.7532"},"ProductsCharges":{"Product":{"TariffCode":"5109.90.80.00","Charges":{"Duties":"75.0021","TaxesAndFees":"0.00","VAT":"0.00","CostOfGoods":"1250.00","SubTotal":"1325.0021"}},"ProductsSubTotal":"1325.0021"},"TotalLandedCost":"1326.7553"},"SuppressQuestionIndicator":"Y"}';
$array = json_decode($json, true);
echo $array['ShipmentEstimate']['ProductsCharges']['Product']['TariffCode'];
See it in action here: https://3v4l.org/2WBs6

PHP get variable from json results

Using PHP I have a json result of
{"text_block":[{"text":"XYZ","left":0,"top":0,"width":10,"height":12}]}
I can get this printed by the below code:
$json= file_get_contents('https://api.url');
$result = json_decode($json, true); //this returns an array
$result = json_decode($json);
$data = get_object_vars(json_decode($json));
$data = array_slice( $data, 0, 10 ); // now you can array functions
echo json_encode( $data );
but need to put the text "XYZ" into a variable for further use in PHP script. How can I do this, I've check various sources but don't seem to be getting anywhere! Thanks
$res = json_decode($data,true);
$my_text = $res['text_block'][0]['text'];
now you can use my_text as a variable
It looks like the value "XYZ" would be in
$data["text_block"][0]["text"]
Temporarily change the last line to var_dump($data) so you can get familiar with the php array structure.

GET info from external Array/API/URL using PHP

I have the url http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132 which leads to an array.
I want to get the value of the first 'sellorders' which in this case is: 0.00000048 and store it in the variable $sellorderprice.
Can anyone help?
Thanks.
Just access the url contents thru file_get_contents. Your page actually return a JSON string, to get those values into meaningful data, decode it thru json_decode, after that access the data needed accordingly:
$url = 'http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132';
$data = json_decode(file_get_contents($url), true);
$sellorderprice = $data['return']['DOGE']['sellorders'][0]['price'];
echo $sellorderprice;
That code actually points directly to index zero 0 which gets the first price. If you need to get all items an just simply echo them all you need to iterate all items thru foreach:
foreach($data['return']['DOGE']['sellorders'] as $sellorders) {
echo $sellorders['price'], '<br/>';
}
Its simple, you just have to decode json like this:
$json = file_get_contents("http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132");
$arr = json_decode($json, true);
$sellorderprice = $arr['return']['DOGE']['sellorders'][0]['price'];

How to get a thing from a Json text?

i want to know how can i extract a word from a json encoded or decoded.
Example:
From:
{"51973658":{"id":51973658,"name":"Covrigel","profileIconId":748,"summonerLevel":30,"revisionDate":1419865098000}}
I want to see just "Covrigel".
Is that possible?
This is a simple array. You can access that by something like this,
echo $array_name['51973658']['name'];
EDIT after question change:
$json = json_decode($json_array, true);
echo $json['51973658']['name'];
All you have to do is convert it to a PHP array.
$json = '{"51973658":{"id":51973658,"name":"Covrigel","profileIconId":748,"summonerLevel":30,"revisionDate":1419865098000}}';
$json = json_decode($json, $array = true);
echo $json['51973658']['name'];
Decode the JSON as an associative array then echo the value you need.
<?php
$json = '{"51973658":{"id":51973658,"name":"Covrigel","profileIconId":748,"summonerLevel":30,"revisionDate":1419865098000}}';
$data = json_decode($json, true);
echo $data['51973658']['name'];
?>

Assign Key to Existing Array

Assuming i have a JSON array like this example below :
[{"type":"food","alias":"meal"}]
And i want to assign this whole array to a particular key called "Dish" for example.
How can i archive this ?
Expected output should be something like :
"Dish":[{"type":"food","alias":"meal"}]
I know how to create new key value pairs but never thought of assigning a key until now.
$json = '[{"type":"food","alias":"meal"}]';
$data = json_decode($json, true);
$data = array('Dish' => $data);
echo json_encode($data);
You could do like this..
<?php
$json = '[{"type":"food","alias":"meal"}]';
$arr = array('dish'=>json_decode($json,true));
echo json_encode($arr);
Demo
echo json_encode(array('Dish' => json_decode($json, true)));
//{"Dish":[{"type":"food","alias":"meal"}]}

Categories