issue with array and json decode - php

I'm a little confused I have the following;
array(4) { ["id"]=> string(1) "2" ["result"]=> array(5) { ["account"]=> string(34) "rf2DKsfbZuCa2WwQAg49cRXqYuRCTszUTp" ["assets"]=> array(1) { ["rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ"]=> array(1) { [0]=> array(2) { ["currency"]=> string(3) "FLX" ["value"]=> string(9) "999999999" } } } ["ledger_hash"]=> string(64) "E44084AAC1F1AE2C7147675BF8F763ED7620DC82E1B1A5BD6DC6F640652F1B63" ["ledger_index"]=> int(17773072) ["validated"]=> bool(true) } ["status"]=> string(7) "success" ["type"]=> string(8) "response" }
I am trying to echo the currency;
I have the following but it's not working.
$response = websocket_read($sp,$errstr);
$decode = json_decode($response, true);
echo'<br><br>';
echo $decode['result']['assets']['currency'];
echo'<br><br>';
echo "Server responed with: '" . $response ."'\n";
The line that is not working is echo $decode['result']['assets']['currency'];
Thankyou

Your rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ has the value as an array, either remove the square brackets from this part to treat it as value
"rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ":[{"currency":"FLX","value":"999999999"}]
to
"rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ":{"currency":"FLX","value":"999999999"}
or use index to get the value
echo $decode['result']['assets']['rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ'][0]['currency'];

Related

How to get facebook album photos id and time using php?

I'm new in php. I make below two code to get facebook album photos id and create time. But its not working and don't showing me result/ID, only showing me blank page.
Here is my two code with $json and $array output.
Code 1;
<?php
$token="<token>";
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos?fields=id&access_token=$token");
$json = json_decode($data);
echo $json->id;
echo $json->created_time;
?>
Code 1 Output: using var_dump($json);
object(stdClass)#1 (2) {
["data"]=>
array(3) {
[0]=>
object(stdClass)#2 (2) {
["id"]=>
string(15) "160246594547781"
["created_time"]=>
string(24) "2017-08-04T18:09:13+0000"
}
[1]=>
object(stdClass)#3 (2) {
["id"]=>
string(15) "160246581214449"
["created_time"]=>
string(24) "2017-08-04T18:09:12+0000"
}
[2]=>
object(stdClass)#4 (2) {
["id"]=>
string(15) "160246587881115"
["created_time"]=>
string(24) "2017-08-04T18:09:13+0000"
}
}
["paging"]=>
object(stdClass)#5 (1) {
["cursors"]=>
object(stdClass)#6 (2) {
["before"]=>
string(20) "MTYwMjQ2NTk0NTQ3Nzgx"
["after"]=>
string(20) "MTYwMjQ2NTg3ODgxMTE1"
}
}
}
Code 2:
<?php
$token="<token>";
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos?fields=id&access_token=$token");
$array = json_decode($data, true);
echo $array['data']['id'];
echo $array['data']['created_time'];
?>
Code 2 Output: Using var_dump($array);
array(2) {
["data"]=>
array(3) {
[0]=>
array(2) {
["id"]=>
string(15) "160246594547781"
["created_time"]=>
string(24) "2017-08-04T18:09:13+0000"
}
[1]=>
array(2) {
["id"]=>
string(15) "160246581214449"
["created_time"]=>
string(24) "2017-08-04T18:09:12+0000"
}
[2]=>
array(2) {
["id"]=>
string(15) "160246587881115"
["created_time"]=>
string(24) "2017-08-04T18:09:13+0000"
}
}
["paging"]=>
array(1) {
["cursors"]=>
array(2) {
["before"]=>
string(20) "MTYwMjQ2NTk0NTQ3Nzgx"
["after"]=>
string(20) "MTYwMjQ2NTg3ODgxMTE1"
}
}
}
Please help me to solve this issue.thanks
For first one you need to do:-
<?php
$token="<token>";
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos?fields=id&access_token=$token");
$json = json_decode($data);
foreach($array.data as $arr){
echo $arr.id; echo PHP_EOL; // you can use `echo "<br/>";`
echo $arr.created_time;echo PHP_EOL;// you can use `echo "<br/>";`
}
?>
For second-one you can use:-
<?php
$token="<token>";
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos?
fields=id&access_token=$token");
$array = json_decode($data, true);
foreach($array['data'] as $arr){
echo $arr['id']; echo PHP_EOL; // you can use `echo "<br/>";`
echo $arr['created_time'];echo PHP_EOL;// you can use `echo "<br/>";`
}
?>
Output:- https://eval.in/841721

How to parse steam api JSON and print it out? PHP

I want to parse JSON that I recieve from steam via api link http://steamcommunity.com/id/theorangeass/inventory/json/753/1/ , but when I try to print it with echo it show nothing.
Here is the code
$data = file_get_contents('http://steamcommunity.com/id/theorangeass/inventory/json/753/1/');
$json = json_decode($data, true);
echo $json->success;
var_dump:
array(6) { ["success"]=> bool(true) ["rgInventory"]=> array(1) { ["922506184369981039"]=> array(5) { ["id"]=> string(18) "922506184369981039" ["classid"]=> string(10) "1254492673" ["instanceid"]=> string(10) "2070301907" ["amount"]=> string(1) "1" ["pos"]=> int(1) } } ["rgCurrency"]=> array(0) { } ["rgDescriptions"]=> array(1) { ["1254492673_2070301907"]=> array(18) { ["appid"]=> string(3) "753" ["classid"]=> string(10) "1254492673" ["instanceid"]=> string(10) "2070301907" ["icon_url"]=> string(116) "U8721VM9p9C2v1o6cKJ4qEnGqnE7IoTQgZI-VTdwyTBeimAcIowbqB-harb00cJ0fNdiCJoFB3O541FNc9ZPYXYjjL7UqfFEwOtgZKcs0eWlClqzSJn6" ["icon_url_large"]=> string(106) "U8721VM9p9C2v1o6cKJ4qEnGqnE7IoTQgZI-VTdwyTBeimAcIowbqB-harb00cJ0fNdiA54UEGOnqGQPJ9hDZHA50feEo7RMyO_GQNzkkA" ["icon_drag_url"]=> string(0) "" ["name"]=> string(4) "BEEP" ["market_name"]=> string(0) "" ["name_color"]=> string(0) "" ["background_color"]=> string(0) "" ["type"]=> string(4) "Gift" ["tradable"]=> int(0) ["marketable"]=> int(0) ["commodity"]=> int(0) ["cache_expiration"]=> string(20) "2017-01-02T00:00:00Z" ["fraudwarnings"]=> array(1) { [0]=> string(223) "This is a restricted gift which can only be redeemed in these countries: Armenia, Azerbaijan, Belarus, Georgia, Kyrgyzstan, Kazakhstan, Moldova, Republic of, Tajikistan, Turkmenistan, Uzbekistan, Ukraine, Russian Federation" } ["descriptions"]=> array(1) { [0]=> array(1) { ["value"]=> string(216) "A combination of Yoshi’s Island-style platforming with a gravity gun right out of Half-Life, BEEP is an amazing physics-platformer. Despite its friendly art style, this is a hardcore platformer in the truest sense." } } ["actions"]=> array(1) { [0]=> array(2) { ["name"]=> string(13) "View in store" ["link"]=> string(41) "http://store.steampowered.com/app/104200/" } } } } ["more"]=> bool(false) ["more_start"]=> bool(false) }
You are trying to echo a boolean. If you are wanting to echo true you're going to need to do an if, or a switch statement. An if statement would probably be easiest.
echo $json->success == true ? 'TRUE' : 'FALSE';
To parse the array to retrieve the item id's in the rgInventory to must do a foreach.
foreach ($json['rgInventory'] as $item) {
echo $item['id'];
}
Where you should start learning about arrays is Here

How to refer to data in an array (php/json)

I am obtaining a json object using the following:
$json = file_get_contents("url-here");
$data = json_decode($json, true);
//test
var_dump($data);
This gives me something like this:
array(2) { ["ok"]=> bool(true) ["result"]=> array(1) { [0]=> array(2)
{ ["update_id"]=> int(44893465) ["message"]=> array(5) {
["message_id"]=> int(16) ["from"]=> array(3) { ["id"]=> int(29595794)
["first_name"]=> string(3) "Bob" ["username"]=> string(14) "Bobo" }
["chat"]=> array(3) { ["id"]=> int(29595794) ["first_name"]=>
string(3) "Bob" ["username"]=> string(14) "Bobo" } ["date"]=>
int(1435354253) ["text"]=> string(7) "/q 3.33" } } } }
I would then like to add certain values into variables. For example I would like to extract username, text, message_id, etc
But no matter what I try my variables are empty:
//let's test it
echo "Username: " . $data[1][0]["username"];
//another test
echo $data->username;
Neither of these are working and my research has not helped me find a solution. I am stumped on this one.
Any pointers in the right direction would really be appreciated.
array(2) {
["ok"]=> bool(true)
["result"]=> array(1)
{
[0]=> array(2)
{
["update_id"]=> int(44893465)
["message"]=> array(5)
{
["message_id"]=> int(16)
["from"]=> array(3)
{
["id"]=> int(29595794)
["first_name"]=> string(3) "Bob"
["username"]=> string(14) "Bobo"
}
["chat"]=> array(3)
{
["id"]=> int(29595794)
["first_name"]=> string(3) "Bob"
["username"]=> string(14) "Bobo"
}
["date"]=> int(1435354253)
["text"]=> string(7) "/q 3.33"
}
}
}
}
You are using wrong array index. $data[1][0]["username"]; not exists.
$data["result"][0]["message"]["from"]["username"];
$data["result"][0]["message"]["chat"]["username"];
This will give you the proper username
$json = file_get_contents("url-here");
$data = json_decode($json, true);
//test
echo $data["result"][0]['message']['from']['username'];
output
Bobo

JSON data parsing cannot understand

array(2) {
["success"]=>
string(1) "1"
["return"]=>
array(125) {
[0]=>
array(11) {
["marketid"]=>
string(3) "141"
["label"]=>
string(6) "42/BTC"
["primary_currency_code"]=>
string(2) "42"
["primary_currency_name"]=>
string(6) "42Coin"
["secondary_currency_code"]=>
string(3) "BTC"
["secondary_currency_name"]=>
string(7) "BitCoin"
["current_volume"]=>
string(10) "0.11628537"
["last_trade"]=>
string(12) "223.00000000"
["high_trade"]=>
string(12) "256.88999999"
["low_trade"]=>
string(12) "205.00000000"
["created"]=>
string(19) "2014-01-12 19:35:49"
}
}
I was trying to process this in PHP
$result = api_query("getmarkets");
$json = json_decode($result);
var_dump($result);
I tried many times to start processing this data but how would I start this
I was thinking something like $json['return'][0]['marketid'] would grab the market id.
Looks like it is already php object, so no need to parse as json.
try...
$result['return'][0]['marketid']
... and delete ...
$json = json_decode($result);

Converting Json String into PHP array then using php array

Im new to json & php and I'm having some issues with json into php string
My json string looks like this
{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}
So Far i have created my string
$json_raw = '{"status":"OK","cards": [{"id":100001,"name": .....
Decoded the json
$arr = json_decode($json_raw, TRUE);
I var_dump($arr);
then it returns
array(2) { ["status"]=> string(2) "OK" ["cards"]=> array(4) { [0]=> array(8) { ["id"]=> int(100001) ["name"]=> string(6) "batman" ["image"]=> int(11111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T11:37:07Z" } [1]=> array(8) { ["id"]=> int(100002) ["name"]=> string(8) "superman" ["image"]=> int(111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:30:09Z" } [2]=> array(8) { ["id"]=> int(100003) ["name"]=> string(8) "catwoman" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:39:42Z" } [3]=> array(8) { ["id"]=> int(100004) ["name"]=> string(4) "bane" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-09-08T12:56:04Z" } } }
Now all I want to do is be able to use this data
e.g if name = batman then
I know this is a stupid question but I am struggling :(
Thank in Advance
json_decode() with TRUE as second parameter gives you an associative array. You need to access the correct index to do what you want.
To list the complete associative array with nice formatting, you can do:
echo '<pre>', print_r($arr), '</pre>';
Now, to access the name in your array:
$man = $arr['cards'][0]['name'];
To check if it's Batman (yay!):
if( isset($man) && $man == 'batman' ) {
# code ...
}
For getting the name of all similar names:
$man = $json['cards']['0']['name'];
for ($i=0; $i < count($json['cards']); $i++) {
echo $json['cards'][$i]['name']."\n";
}
See it live!
when you got the array
$arr = json_decode($json_raw, TRUE);
then check if cards key exist
if(array_key_exists('cards', $arr)){
foreach($arr['cards'] as $key=>$val){
echo $key; ///name, id..
echo $val; /// batman,...
if($key == 'name' && $val =='batman'){
//-------do your stuff
}
}
}
Try with:
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
EDIT:
Ok, so this worked for me using code above, try it yourself if you want:
<?php
$json_raw = '{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}';
$arr = json_decode($json_raw, TRUE);
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
?>

Categories