how to access object array as show in code [duplicate] - php

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
how to access each file name...
{"Appointments":
[
{
"file_name":"eeea4a560492004c5c14f9414e88c32c.jpg",
"file_type":"image\/jpeg",
"file_path":"C:\/inetpub\/wwwroot\/nautSearch\/server\/uploadedfiles\/files\/",
"orig_name":"a46cf6866df6f358bece629ef472ac69.jpg",
"file_ext":".jpg",
"file_size":418.17,
"is_image":true,
"image_width":2560,
"image_height":1440,
"image_type":"jpeg",
"image_size_str":"width=\"2560\" height=\"1440\""
},
{
"file_name":"c08ff85a44e0f86ab48ec709e6ba1b4a.jpg",
"file_type":"image\/jpeg",
"file_path":"C:\/inetpub\/wwwroot\/nautSearch\/server\/uploadedfiles\/files\/",
"file_ext":".jpg",
"file_size":27.38,
"is_image":true,
"image_width":479,
"image_height":403,
"image_type":"jpeg",
"image_size_str":"width=\"479\" height=\"403\""
},
{
"file_name":"4536acd0479c6c5dee3b1f546ed8d328.jpg",
"file_type":"image\/jpeg",
"file_path":"C:\/inetpub\/wwwroot\/nautSearch\/server\/uploadedfiles\/files\/",
"file_ext":".jpg",
"file_size":27.38,
"is_image":true,
"image_width":479,
"image_height":403,
"image_type":"jpeg",
"image_size_str":"width=\"479\" height=\"403\""
}
]
}

What you gave is a json string. First you must decode it and loop over the appointments to use the file names:
$result = json_decode($yourJsonString);
foreach ($result->Appointments as $appointment) {
echo $appointment->file_name;
}

Related

How to extract a series of variables from string of data in php? [duplicate]

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/>';
}

Fetching data from Key Value JSON object [duplicate]

This question already has answers here:
Why can't Python parse this JSON data? [closed]
(3 answers)
Closed 4 years ago.
I have a JSON object below, but i'm struggling to get name, value from it.
I would want to get the values and save them into a database.
$json = '
{
"Body":
{
"stkCallback":
{
"ResultCode":0,
"ResultDesc":"The service request is processed successfully.",
"CallbackMetadata":
{
"Item":
[
{
"Name":"Amount",
"Value":10
},
{
"Name":"TransactionDate",
"Value":20170727154800
},
{
"Name":"PhoneNumber",
"Value":26721566839
}
]
}
}
}
}';
How can I get the "Name":"Amount" values and "Name":"TransactionDate" value.
At least for the rest
$ResultCode = json_decode($json)->Body->stkCallback->ResultCode;
$ResultDesc = json_decode($json)->Body->stkCallback->ResultDesc;
The question is, how do I get the values in a loop when some have values and others don't?
Try this :
$response=json_decode($json,true);
echo $amount=$response['Body']['stkCallback']['CallbackMetadata']['Item']['0']['Value'];
echo $TransactionDate=$response['Body']['stkCallback']['CallbackMetadata']['Item']['1']['Value'];
echo $PhoneNumber=$response['Body']['stkCallback']['CallbackMetadata']['Item']['2']['Value'];
You have all the values in variables now.

How to detect whether a JSON object is a String or an Array in PHP? [duplicate]

This question already has answers here:
How to check if variable is array?... or something array-like
(7 answers)
Closed 6 years ago.
When I have an input like below...
{
"number":[
"+39XXXXXXXX",
"+34XXXXXXXX",
"+49XXXXXXXX"
],
"message":"Sample msg..."
}
I handle it with a foreach loop—like so:
foreach ($message->number as $key => $number) {
...
}
However when I have an input like this:
{
"number": "+49XXXXXXXX",
"message": "Sample msg..."
}
I receive an error, cause there is no array to be looped inside the object.
So what is a good and efficient way to detect for this?
You can check if the var value is array using the is_array function:
if (is_array($message->number) {
foreach ($message->number as $key => $number) {
...
}
} else {
...
}

Want to get one value from json result using php [duplicate]

This question already has answers here:
Get data from JSON file with PHP [duplicate]
(3 answers)
Closed 7 years ago.
How can i get ID value only?
like so here: $fo = "11890408";
{"success":true,"result":{"success":[{"id":"11890408","device_id":"17183","message":"okey","status":"pending","send_at":1455046054,"queued_at":0,"sent_at":0,"delivered_at":0,"expires_at":1455049654,"canceled_at":0,"failed_at":0,"received_at":0,"error":"","created_at":1455046054,"contact":{"id":"2522330","name":"923336458112","number":"923336458112"}}],"fails":[]}}
Convert the json string to a php data structure and then navigate through the data tree of objects and arrays:
$json = '{
"success":true,
"result":{
"success[
{
"id":"11890408",
"device_id":"17183",
"message":"okey",
"status":"pending",
"send_at":1455046054,
"queued_at":0,
"sent_at":0,
"delivered_at":0,
"expires_at":1455049654,
"canceled_at":0,
"failed_at":0,
"received_at":0,
"error":"",
"created_at":1455046054,
"contact":{
"id":"2522330",
"name":"923336458112",
"number":"923336458112"
}
}
],
"fails":[]
}
}';
$data = json_decode($json);
$fo = $data->result->success[0]->id;

Create array with only tag - Json decode [duplicate]

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);

Categories