How can I access .json data in PHP? - php

I have a .json file. I want to access this data in my .php file. I have a variable $spellId, for example: $spellId="SummonerBarrier";
I want to get "name" from the .json file just by having $spellId.
Any idea how I can do this?
I know there are a lot of questions like this here, but i do not how to make the solutions with my code.
{
"type": "summoner",
"version": "6.3.1",
"data": {
"SummonerBarrier": {
"id": "SummonerBarrier",
"name": "Barrier",
"description": "Shields your champion for 115-455 (depending on champion level) for 2 seconds.",
"tooltip": "Temporarily shields {{ f1 }} damage from your champion for 2 seconds.",
"maxrank": 1,
"cooldown": [
210
]
},
"SummonerBoost": {
"id": "SummonerBoost",
"name": "Cleanse",
"description": "Remove..."
}
}
}

Try this:
$data = '{
"type": "summoner",
"version": "6.3.1",
"data": {
"SummonerBarrier": {
"id": "SummonerBarrier",
"name": "Barrier",
"description": "Shields your champion for 115-455 (depending on champion level) for 2 seconds.",
"tooltip": "Temporarily shields {{ f1 }} damage from your champion for 2 seconds.",
"maxrank": 1,
"cooldown": [
210
]
},
"SummonerBoost": {
"id": "SummonerBoost",
"name": "Cleanse",
"description": "Remove..."
}
}
}';
$decoded = json_decode($data);
var_dump($decoded->data->SummonerBarrier->name);

$array = json_decode($json, true);
foreach ($array['data'] as $key => $value) {
echo "$key:$value[name]<br />";
}

$decodeData = json_decode($data);
$spellId="SummonerBarrier";
$nameToFetch=$decodeData->data->$spellId->name;

Related

How to get name in json coinmarketcap? PHP

How to get name if I put BTC ?
$url = file_get_contents(https://api.coinmarketcap.com/v1/ticker/);
$json = json_decode($url,true);
and result
[
{
"id": "bitcoin",
"name": "Bitcoin",
"symbol": "BTC",
},
{
},
{
"id": "ethereum",
"name": "Ethereum",
"symbol": "ETH",
},
{
"id": "bitcoin-cash",
"name": "Bitcoin Cash",
"symbol": "BCH",
},
]
eg.
when I put "BTC"
and need output = "Bitcoin"
or put "ETH"
output = "Ethereum"
You can find proper record using:
$key = array_search('BTC', array_column($json, 'symbol'));
And then (if $key found) get the record like this:
$record = $json[$key];
var_dump($record);
Probalby u need to use full coins name (lowercase) as described in official docs https://coinmarketcap.com/api/ look into paragraph Ticker (Specific Currency)

json, php - output string from array

I am trying to decode JSON data to PHP then output it to the site. If I have the following:
{
"name": "josh",
"type": "human"
{
I can do this (within PHP), to display or output my type:
$file = "path";
$json = json_decode($file);
echo $json["type"]; //human
So, if I have the following:
{
"name": "josh",
"type": "human",
"friends": [
{
"name": "ben",
"type": "robot"
},
{
"name": "tom",
"type": "alien"
}
],
"img": "img/path"
}
How can I output what type my friend ben is?
Use a loop like foreach and do something like the following:
//specify the name of the friend like this:
$name = "ben";
$friends = $json["friends"];
//loop through the array of friends;
foreach($friends as $friend) {
if ($friend["name"] == $name) echo $friend["type"];
}
To get the decoded data in array format you would supply true as the second argument to json_decode otherwise it will use the default which is object notation. You could easily create a function to shorten the process when you need to find a specific user
$data='{
"name": "josh",
"type": "human",
"friends": [
{
"name": "ben",
"type": "robot"
},
{
"name": "tom",
"type": "alien"
}
],
"img": "img/path"
}';
$json=json_decode($data);
$friends=$json->friends;
foreach( $friends as $friend ){
if( $friend->name=='ben' )echo $friend->type;
}
function finduser($obj,$name){
foreach( $obj as $friend ){
if( $friend->name==$name )return $friend->type;
}
}
echo 'Tom is a '.finduser($friends,'tom');
try this,
$friend_name = "ben";
$json=json_decode($data);
$friends=$json->friends;
foreach( $friends as $val){
if($friend_name == $val->name)
{
echo "name = ".$val->name;
echo "type = ".$val->type;
}
}
DEMO

PHP decoding json and display it

I need to decode a json file and display it in the page..
My code is not working, and i don't know whats wrong.
The output is just a blank page.
Here is the json file..
{
"data": [
{
"name": "Jhaimee",
"uid": 10000
},
{
"name": "Ally",
"uid": 10000133
},
{
"name": "Macers",
"uid": 1000056
},
{
"name": "Diego",
"uid": 100004
},
{
"name": "Killersmile",
"uid": 1000050
},
{
"name": "Joel",
"uid": 1000011
}
]
}
I want the output to be..
Jhaimee
Ally
Macers
Diego
Killersmile
Joel
Here is my php.
$data = file_get_contents("https://graph.facebook.com/fql?q=SELECT name,uid FROM user WHERE uid IN (SELECT recipients FROM thread WHERE folder_id = 0 ORDER BY message_count DESC) AND uid != me() LIMIT 10&access_token=xxxx");
$data = json_decode($data, true);
echo $data[0]["name"];
The outermost array key should be data
echo $data['data'][0]['name']
foreach($data['data'] as $person){ echo $person['name'] . "<br />\n"; }

Filtering JSON Object and outputting another

I'm trying to parse a JSON file which looks like this,
[
{
"id": "539eebdba276db40a4716726",
"name": "Development Task",
"idList": "539eebbb4e9a8d709704b254",
"desc": "",
"url": ""
},
{
"id": "539eebe09b42c971d46b9ba1",
"name": "Design Task",
"idList": "539eebbe50dc4fa2a82474fc",
"desc": "",
"url": ""
}
]
I'm trying to get the desc object from the array with the name Development Task, the system needs to be dynamic so I can't just use json_o[0](desc);
I've tried different methods such as foreaching the data multiple times but I still can't think of a solution, any help would be great, cheers.
Try with this:
$jsonData = '[
{
"id": "539eebdba276db40a4716726",
"name": "Development Task",
"idList": "539eebbb4e9a8d709704b254",
"desc": "",
"url": ""
},
{
"id": "539eebe09b42c971d46b9ba1",
"name": "Design Task",
"idList": "539eebbe50dc4fa2a82474fc",
"desc": "",
"url": ""
}
]';
$encoded = json_decode($jsonData);
foreach($encoded as $data)
{
if('Development Task' == $data->name)
{
echo $data->desc;
}
}
If you search for dynamic fields, i would have gone with :
$jsonData = '[
{
"id": "539eebdba276db40a4716726",
"name": "Development Task",
"idList": "539eebbb4e9a8d709704b254",
"desc": "FirstDescription",
"url": ""
},
{
"id": "539eebe09b42c971d46b9ba1",
"name": "Design Task",
"idList": "539eebbe50dc4fa2a82474fc",
"desc": "SecondDescription",
"url": ""
}
]';
$arrayFromJson = json_decode($jsonData, true);
function searchByKey($keyToSearchIn, $searchName, $array) {
foreach ($array as $key => $val) {
if ($val[$keyToSearchIn] == $searchName) {
return $val['desc'];
}
}
return null;
}
$return = searchByKey("name", "Development Task", $arrayFromJson);
So
var_dump($return);
returns
string(16) "FirstDescription"
Note : If you only need a search by name, you can change the function with :
function searchByName( $searchName, $array) {
foreach ($array as $key => $val) {
if ($val['name'] == $searchName) {
return $val['desc'];
}
}
return null;
}
$return = searchByName("Development Task", $arrayFromJson);
Hope it helps.
The comments in the code explain what happens:
$jsonData = '[
{
"id": "539eebdba276db40a4716726",
"name": "Development Task",
"idList": "539eebbb4e9a8d709704b254",
"desc": "moocow",
"url": ""
},
{
"id": "539eebe09b42c971d46b9ba1",
"name": "Design Task",
"idList": "539eebbe50dc4fa2a82474fc",
"desc": "wowcow",
"url": ""
},
{
"id": "539eebe09rb42c971d46b9ba1",
"name": "Development Task",
"idList": "539eebbe50dc4fa2a82474fc",
"desc": "sowccow",
"url": ""
}
]';
$encoded = json_decode($jsonData, true); //converts the json object to an array
foreach ($encoded as $arrayObject){
if( in_array("Development Task", $arrayObject) ){
print_r($arrayObject['desc'] . "\n");
}
}
-> moocow sowccow
based upon
I'm trying to get the desc object from the array with the name Development Task
This should do what you want and you can easily take the string and make it a variable. There are plenty of built-in functions on PHP.net that are tuned and are probably 10x better than rolling your own code.

How to get the profile picture of user posts from Facebook API search results?

$search = $facebook->api('/search?q=watermelon&type=post');
returns something like this:
{
"data": [
{
"id": "1194579494_10201162886052970",
"from": {
"name": "Dawn Rittman Stoltz",
"id": "1194579494"
},
"message": "Our first watermelon and pumpkin from the farm!!! We rock at this garden thing",
"picture": "https://fbcdn-photos-f-a.akamaihd.net/hphotos-ak-ash3/563531_10201162883172898_1559330219_t.jpg",
"link": "https://www.facebook.com/photo.php?fbid=10201162883172898&set=pcb.10201162886052970&type=1&relevant_count=2",
"icon": "https://static.xx.fbcdn.net/rsrc.php/v2/yx/r/og8V99JVf8G.gif",
"privacy": {
"value": ""
},
"type": "photo",
"object_id": "10201162883172898",
"application": {
"name": "Facebook for iPhone",
"namespace": "fbiphone",
"id": "6628568379"
}
],
"paging": {
"previous": "https://graph.facebook.com/search?type=post&q=watermelon&limit=25&since=1379461168",
"next": "https://graph.facebook.com/search?type=post&q=watermelon&limit=25&until=1379460365"
}
}
Is there a more efficient way than getting the id of the facebook user and making a bunch of requests to get their profile picture from the api like this?
$data = $search['data'];
$picture_urls = array();
foreach ($data as $status) {
$from = $status['from'];
$id = $from['id'];
$picture = $facebook->api('/' . $id . '/picture?type=square&redirect=false');
$picture_data = $picture['data'];
$picture_url = $picture_data['url'];
array_push($picture_urls, $picture_url);
}
You can get the picture field directly in your first call. Try this-
/search?q=watermelon&type=post&fields=from.picture

Categories