This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I cannot access the contents within the orders array. I am currently doing this to no avail, and am wondering what I am doing wrong:
the $json object is a json response from a rest api.
$orderData = $json['orders'];
foreach($orderData['orders'] as $order){
}
{
"errors":[
],
"orders":[
{
"note":"",
"estimated_shipping_fee":"0.00",
"payment_method":"PAY_CYBERSOURCE",
"escrow_amount":"12.95",
"message_to_seller":"",
"shipping_carrier":"Singpost - Normal Mail",
"currency":"SGD",
"create_time":1532064559,
"pay_time":1532064618,
"recipient_address":{
"town":"",
"city":"",
"name":"Teo",
"district":"",
"country":"SG",
"zipcode":"41253",
"full_address":"In some street somewhjere",
"phone":"23154991",
"state":""
},
"days_to_ship":3,
"tracking_no":"",
"order_status":"SHIPPED",
"note_update_time":0,
"update_time":1532082525,
"goods_to_declare":false,
"total_amount":"12.95",
"service_code":"",
"country":"SG",
"actual_shipping_cost":"",
"cod":false,
"items":[
{
"weight":1.0,
"item_name":"ABC",
"is_wholesale":false,
"item_sku":"5123433123",
"variation_discounted_price":"12.95",
"variation_id":0,
"variation_name":"",
"item_id":1126534500,
"variation_quantity_purchased":1,
"variation_sku":"",
"variation_original_price":"12.95"
}
],
"ordersn":"3589290984539"
}
]
}
Trying to access the variables directly, by say json['orders'][0]['payment_method'] is not working.
convert it to array from json using json_decode(); then you can easily access it and if you want to access using jquery then just convert it object using jQuery.parseJSON();
just use json_decode for retrieving the object and then $obj->note or you can turn it into an array: $array = get_object_vars($obj); like in this answer .
Related
This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 3 years ago.
The following chunk of data is produced by a webpage along with other html data;
"search":{"searchResults":{"results":[
{"id":"123","name":"ABC","rating":{"average":0,"count":2,"__typename":"Rating"},"category":"AAA/Cars","__typename":"ProductQuery"},
{"id":"456","name":"DEF","rating":{"average":5,"count":8,"__typename":"Rating"},"category":"BBB/Bikes","__typename":"ProductQuery"}
{"id": //and so on//
"}
]}}
How to extract multiple variables from this string like data like "id", "rating" etc., to be able to print it on another php page?
You can use json_decode to convert JSON to the array and then iterate through the array
$json = '{
"search":
{
"searchResults":
{
"results":[
{"id":"123","name":"ABC","rating":{"average":0,"count":2,"__typename":"Rating"},"category":"AAA/Cars","__typename":"ProductQuery"},
{"id":"456","name":"DEF","rating":{"average":5,"count":8,"__typename":"Rating"},"category":"BBB/Bikes","__typename":"ProductQuery"}
]
}
}
}';
$jsonToArray = json_decode($json,true);
foreach($jsonToArray['search']['searchResults']['results'] as $v){
echo $v['id'];
print_r($v['rating']);echo '<br/>';
}
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
The API is returning the following json:
{
"car1": [
{
"car_name": "audi",
"gear_box_type": "Automatic",
"num_of_seats": "2",
"num_of_doors": "3",
"imagePath": "/images/a3.png"
}
]
}
The following is my php code:
<select class="form-control" id="exampleFormControlSelect1">
<?php
$obj = new ApiHelper();
$result = $obj->CallAPI("GET","http://localhost:5000/cars", "NONE");
$jsondata = json_decode($result, true);
$car = $jsondata['car1'];
echo "<option>".$car."</option>";
?>
</select>
The output will be "Array".
Image of select
I would like to know how I can access this array that is inside the json.
Any help much appreciated thanks.
The array is accessed like any standard PHP array. You can Google search "PHP arrays" to find more information. One helpful function is print_r($array_name).
So:
print_r($jsondata);
Will give you a printout of the structure of the array, making it easy for you to then determine what is available to you in the array. If you view the output of this function in your browser, be sure to right-click on the page and select "View Source." Otherwise the structure will be printed out as one long string that isn't very fun to read.
Hope this helps!
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
How do I get the uname value from this JSON into PHP variable? to use through my page?
{
"token": "iutiutiut-0jjjjj0-97987g",
"auth": {
"id": 1,
"app_id": 1,
"user": {
"uname": "foo",
"role": "member"
}
}
}
thanks for some reason just can't get it and I can't find examples similar anywhere on google or I am not calling it correctly.
could you also tell me the correct terminology so I know as well thanks
What you're trying to do is decode JSON. PHP has a built in function for this aptly named... json_decode. Assuming your JSON is a string ($json_string), here is how you would decode it:
$obj = json_decode($json_string);
$uname = $obj->auth->user->uname;
Or, if you prefer the array syntax, use the second argument in json_decode:
$arr = json_decode($json_string, true);
$uname = $obj['auth']['user']['uname'];
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 7 years ago.
How do I return only all the urls item "webformatURL" using json decode?
{
"totalHits":500,
"hits":[
{
"previewHeight":84,
"likes":37,
"favorites":42,
"tags":"yellow, natural, flower",
"webformatHeight":360,
"views":11218,
"webformatWidth":640,
"previewWidth":150,
"comments":8,
"downloads":6502,
"pageURL":"https://example.com/en/yellow-natural-flower-715540/",
"previewURL":"https://example.com/static/uploads/photo/2015/04/10/00/41/yellow-715540_150.jpg",
"webformatURL":"https://example.com/get/ee34b40a2cf41c2ad65a5854e4484e90e371ffd41db413419cf3c271a6_640.jpg",
"imageWidth":3020,
"user_id":916237,
"user":"916237",
"type":"photo",
"id":715540,
"userImageURL":"https://example.com/static/uploads/user/2015/04/07/14-10-15-590_250x250.jpg",
"imageHeight":1703
},
],
"total":7839
}
I try:
$test= json_decode($json);
$result= $test->webformatURL;
Error: warning-undefined-property-stdclass::webformatURL
I've read the manual but I doubt then I would see an example in practice
json_decode
Thanks for any help
Did you mean?
$result = $test->hits[0]->webformatURL;
If you want to extract only this field from the object, you can do:
$urls = [];
foreach($test->hits as $hit) {
$urls[] = $hit->webformatURL;
}
var_dupm($urls);
This question already has answers here:
json_decode to array
(12 answers)
Closed 11 months ago.
The json data object below is what's returned from a custom Google search API request. I need to extract each of the "url" elements and place them into an array (using PHP).
myArray = {url1, url2, url3, etc...}
How?
data = '{
"responseData":
{
"results":
[
{
//etc
}
]
}
Am I right that you have JSON string? Use json_decode to decode it. After that you can use
array_map(function($x){
return $x->url;
},$var->responceData->results);
(Requires PHP 5.3 for anonymous function, you can use no anonymous ones if use PHP5.2 or older)
For later versions:
function smth($x){
return $x->url;
}
array_map('smth',$var->responceData->results);
You can use json_decode to get an array corresponding to your JSON and then analyze it like you would do for a normal array.
You might want to read up on json_decode
Try using:
$myObject=json_decode($myJSONstring);
Here's the reference.
Then do:
$urlArray=array();
foreach($myObject->responseData->results as $myResult) {
foreach($myResult as $myAttribute => $myValue) {
$urlArray[] = $myValue;
}
}
$urlArray will be what you're looking for.