how to get words after word in string [duplicate] - php

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 1 year ago.
I've a json response like this:
'{"success":true,"code":200,"message":"Success","data":[{"title":"title1","productKey":"123","thumbnailUrl":"https://example.com/1.jpg","star":0},{"title":"title2","productKey":"234","thumbnailUrl":"https://example.com/2.jpg","star":0}]}'
above code is in a variable as string
I need just product keys: 123,234;
how can I get product keys from this reponse?
Thanks

Use json_decode to turn it into an associative array.
$jsonString = '{"success":true,"code":200,"message":"Success","data":[{"title":"title1","productKey":"123","thumbnailUrl":"https://example.com/1.jpg","star":0},{"title":"title2","productKey":"234","thumbnailUrl":"https://example.com/2.jpg","star":0}]}';
$json = json_decode($jsonString, true);
$productKeys = [];
foreach($json["data"] as $val)
{
$productKeys[] = $val["productKey"];
}

Related

php how to read file json with array including numbers inside each number different data? [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 months ago.
I have output file json but i can't read the properties in array [0]
contacts:
Array(20)
0:
canonical-vid:00000
form-submissions:[]
identity-profiles:[{…}]
is-contact:true
merge-audits:[]
merged-vids:[]
portal-id:000000
properties:{....}
I use this code in php :
$json = file_get_contents('./file,json');
$data = json_decode($json,true);
$firstname = $data['contacts'][0]['properties']['firstname']['value'];
I think $data['contacts'][0]['properties'] is json. Try like this:
$properties = $data['contacts'][0]['properties'];
$nameData = json_decode($properties,true);

how to get value from array [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
how to get value from array in php from api
here is my code
$data = callBitAPI('GET','https://min-api.cryptocompare.com/data/pricemultifull?fsyms='.strtoupper($bc->slug).'&tsyms=USD');
if(!empty($data)){
$data = json_decode($data,true);
// echo '<pre>';
// print_r($data);
$data = getRelevantCryptoArray($data,$bc->slug);
}
and here is my result
Please see Attachment
http://prntscr.com/k7hawi
Um... simply
$price = $data['RAW']['BTC']['USD']['PRICE'];
print_r($price);
unless I'm missing something?
dd($data['RAW']['BTC']['USD']['PRICE']) and see the result

How can I access the name value in nested JSON using PHP? [duplicate]

This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
How can I access an array/object?
(6 answers)
Closed 6 years ago.
JSON:
{"location":{"name":"Tirana","region":"Tirane","country":"Albania","lat":41.33,"lon":19.82,"tz_id":"Europe/Tirane","localtime_epoch":1484543668,"localtime":"2017-01-16 5:14"},"current":{"last_updated_epoch":1484543668,"last_updated":"2017-01-16 05:14","temp_c":4.0,"temp_f":39.2,"is_day":0,"condition":{"text":"Overcast","icon":"//cdn.apixu.com/weather/64x64/night/122.png","code":1009},"wind_mph":6.9,"wind_kph":11.2,"wind_degree":150,"wind_dir":"SSE","pressure_mb":1009.0,"pressure_in":30.3,"precip_mm":0.0,"precip_in":0.0,"humidity":60,"cloud":0,"feelslike_c":1.2,"feelslike_f":34.2}}
PHP:
$response = file_get_contents('http://api.apixu.com/v1/current.json?key=a54959ce2b294134bda34330171601&q=Paris');
var_dump(json_decode($response));
echo $response->location[0]->name;
API Call: http://api.apixu.com/v1/current.json?key=a54959ce2b294134bda34330171601&q=Tirana
Try like this.You get the contents in json format.Use json_decode() with second parameter true to convert into array.
<?php
$json = '{"location":{"name":"Tirana","region":"Tirane","country":"Albania","lat":41.33,"lon":19.82,"tz_id":"Europe/Tirane","localtime_epoch":1484543668,"localtime":"2017-01-16 5:14"},"current":{"last_updated_epoch":1484543668,"last_updated":"2017-01-16 05:14","temp_c":4.0,"temp_f":39.2,"is_day":0,"condition":{"text":"Overcast","icon":"//cdn.apixu.com/weather/64x64/night/122.png","code":1009},"wind_mph":6.9,"wind_kph":11.2,"wind_degree":150,"wind_dir":"SSE","pressure_mb":1009.0,"pressure_in":30.3,"precip_mm":0.0,"precip_in":0.0,"humidity":60,"cloud":0,"feelslike_c":1.2,"feelslike_f":34.2}}
';
$array = json_decode($json,true);
//print_r($array);
$location = $array['location'];
echo $location['name'];
?>
use json_decode to parse the json string to array, then access it with the index.
the demo
$response = file_get_contents('http://api.apixu.com/v1/current.json?key=a54959ce2b294134bda34330171601&q=Paris');
$array = json_decode($response, true);
echo $array['location']['name'];

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

convert array got as string from "file_get_contents()" back to array [duplicate]

This question already has answers here:
How to convert JSON string to array
(17 answers)
Closed 8 years ago.
When I try to get data from an API using
file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDINR%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");
I got the result as a string. Is there any way to convert it back to array?
The string which I got is
string(202) "{"query":{"count":1,"created":"2014-03-11T13:00:31Z","lang":"en-US","results":{"rate":{"id":"USDINR","Name":"USD to INR","Rate":"60.99","Date":"3/11/2014","Time":"9:00am","Ask":"61.00","Bid":"60.98"}}}}"{"error":"","msg":""}
please help me....
In your request, you ask that the returned format be JSON-encoded (via the format=json parameter), so you can just decode the response into an array using json_decode:
$response = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDINR%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");
$response = json_decode($response, true);
// Parse the array as you would any other
You have a JSON answer here so jo need to dekode that.
<?php
$s = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDINR%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");$data =
$data = json_decode($s,true);
?>
and the $data variable will be an array.

Categories