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

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;

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

How to format a json from an URL on php? [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I got a JSON from an URL like this:
http://myweb.com/ws_clc/ws_info.php?uid=77317
JSON:
{
"status":1,
"info":[
{
"uid":"77317",
"name":"Jack",
"lastname":"Black",
"email":"jackblack#mail.com"
}
]
}
I need to format that JSON on HTML.
¿How can I do that?
THANKS!!! I did it like this:
<?php
$uid =$_GET['uid'];
$json = file_get_contents('http://myweb.com/ws_clc/ws_info.php?uid='.$uid);
$obj = json_decode($json);
$uid= $obj->info[0]->uid;
$name = $obj->info[0]->name;
$lastname = $obj->info[0]->lastname;
echo $name;
echo $lastname;
?>

How do I decode this Json array using PHP [duplicate]

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 decode this json data using php to get individual values.
{"Alpha":["A","B","C","D","E","F"],"Beta":["1","2","3"],"Gamma": ["a","b","c"]}
php has a function called json_decode
$json = '{"Alpha":["A","B","C","D","E","F"],"Beta":["1","2","3"],"Gamma": ["a","b","c"]}';
$json_array = json_decode($json,TRUE); // personally I prefer arrays which the TRUE adds
$alpha = $json_array['Alpha'];
$first_alpha_value = $alpha[0];
printf("A == %s\n",$first_alpha_value);

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

Json php decoding issue [duplicate]

This question already has answers here:
Read Json/Array string in Php
(2 answers)
Closed 8 years ago.
In fact Im having some troubles with php script that i have made recently. The problem is that I can't get data from a json file damo.json using php. This is the code of the json file:
{ "checkouts":[
{
"billing_address":{
"country":"Italy",
"first_name":"christian"
}
},
{
"billing_address":{
"country":"Italy",
"first_name":"christian"
}
}
]
}
i want to get the first_name record. Is it possible using php ?
this is the php code:
<?php
$data = file_get_contents('demo.json');
$obj = json_decode($data);
foreach ($obj->billing_address as $result)
{
echo $result->name;
}
?>
After you fix your syntax as noted above load the file and use json_decode with first parameter the json and second parameter true for associative array.
$data = file_get_contents( "demo.json" );
$data = json_decode($data, true);
var_dump($data);//you have assoc array

Categories