How do I decode this Json array using PHP [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 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);

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 Json Objects inside the Json Array and store it in MySql In php [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I want get Json objects from the json array and store each values in mysql using php.
$arr = "[{"fruit":"Apples","isSelected":false,"number":0},{"fruit":"Oranges","isSelected":false,"number":0},{"fruit":"Potatoes","isSelected":false,"number":0},{"fruit":"Tomatoes","isSelected":false,"number":0},{"fruit":"Grapes","isSelected":false,"number":0},{"fruit":"Dhanana","isSelected":false,"number":0}]";
try this code
Using json_decode
<?php
$arr = '[{"fruit":"Apples","isSelected":false,"number":0},{"fruit":"Oranges","isSelected":false,"number":0},{"fruit":"Potatoes","isSelected":false,"number":0},{"fruit":"Tomatoes","isSelected":false,"number":0},{"fruit":"Grapes","isSelected":false,"number":0},{"fruit":"Dhanana","isSelected":false,"number":0}]';
$data = json_decode($arr);
echo "<pre>";
print_r($data);
foreach($data as $k=>$v){
echo $v->fruit." ".$v->isSelected." ".$v->number."<br>"; //store in db here using value
print_r($v);
}
?>

How to make json objects within array in php [duplicate]

This question already has answers here:
How to create an array for JSON using PHP?
(8 answers)
Closed 8 years ago.
I want to make json as followsin php:
{["key1":"value1"],["key2":"value2"]}
Can any one help?
See the PHP manual for json_encode() and json_decode():
http://php.net/manual/en/function.json-encode.php
http://php.net/manual/en/function.json-decode.php
You can do it like this: set it to true, if you want an associative array.
$array = json_decode('[{"var1":"9","var2":"16","var3":"16"},{"var1":"8","var2":"15","var3":"15"}]',true);
if you want to iterate:
foreach($array as $item) { //foreach element in $arr
$uses = $item['var1']; //etc
}

How i can take the json's value in PHP? [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 8 years ago.
I would like to know, how take the value "gamertag" from this Json (http://360api.chary.us/?gamertag=superdeder) with PHP.
i would print only the value "superdeder".
thanks a lot.
Andrea.
Use file_get_contents() and convert your JSON to an object with json_decode():
$json = file_get_contents('http://360api.chary.us/?gamertag=superdeder');
$gamer = json_decode($json);
echo $gamer->Gamertag; // SuperDeder
Or, an array, by passing a truthy value as json_decode()s second parameter:
$gamer = json_decode($json, true);
echo $gamer['Gamertag']; // SuperDeder

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