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
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
I want to loop through the following array.
array(2) {
[0]=> array(6) {
[0]=> string(10) "excel_id "
[1]=> string(12) "excel_name "
[2]=> string(11) "excel_email"
[3]=> string(13) "excel_address"
[4]=> string(12) "excel_giveby"
[5]=> string(16) "excel_accesories"
}
[1]=> array(6) {
[0]=> float(111)
[1]=> string(10) "sh1exname1"
[2]=> string(11) "sh1exemail1"
[3]=> string(12) "sh1exaddress"
[4]=> string(12) "sh1exgivenby"
[5]=> string(16) "sh1exaccesorries"
}
}
can anyone help me?
If you tried anything at all you should have found foreach.
this will loop through all of your array values.
You can simply loop through a multi-dimensional array by using a multi-dimensional foreach like this:
foreach($array as $parentValue)
{
foreach($parentValue as $childValue)
{
// your code
}
}
try this:
foreach ($main_array as $outer_entry){
foreach($outer_entry as $entity){
print_r($entity);
}
}
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!';
}
?>
Hi I have the following array
array(4) {
[0]=>
array(3) {
["id_acreditado"]=>
string(3) "174"
["cantidad"]=>
string(7) "4008.00"
["acreditado"]=>
string(27) "Olga Olivia Lucio Hernandez"
}
[1]=>
array(3) {
["id_acreditado"]=>
string(3) "175"
["cantidad"]=>
string(7) "4008.00"
["acreditado"]=>
string(23) "Enrique Carranco Vences"
}
[2]=>
array(3) {
["id_acreditado"]=>
string(3) "176"
["cantidad"]=>
string(7) "4008.00"
["acreditado"]=>
string(32) "Juana Patricia Contreras Paredes"
}
[3]=>
array(3) {
["id_acreditado"]=>
string(3) "177"
["cantidad"]=>
string(7) "4008.00"
["acreditado"]=>
string(17) "Noemi Cruz Campos"
}
}
And I want to create a bidimensional array with some values of the above array and different indices. I'm using a foreach loop to achive that.
$j=1;
foreach($acreditados as $acreditado){
$tmp['oneCol'] = $j;
$tmp['twoCol'] = $acreditado['acreditado'];
$tmp['threeCol'] = $acreditado['cantidad'];
$info['fourCol'] =$acreditado['id_acreditado'];
$info[]=$tmp;
$j++;
}
$tmp is an auxiliar one dimension array that lately is added as a row for bidemensional $info array, however I'm not getting the output expected. I want something like the following as output:
array(4) {
[0]=>
array(3) {
["oneCol"]=>
int(1)
["twoCol"]=>
string(27) "Olga Olivia Lucio Hernandez"
["threeCol"]=>
string(7) "4008.00"
["fourCol"]=>
string(3) "174"
}
[1]=>
array(3) {
["oneCol"]=>
int(2)
["twoCol"]=>
string(23) "Enrique Carranco Vences"
["threeCol"]=>
string(7) "4008.00"
["fourCol"]=>
string(3) "175"
}
[2]=>
array(3) {
["oneCol"]=>
int(3)
["twoCol"]=>
string(32) "Juana Patricia Contreras Paredes"
["threeCol"]=>
string(7) "4008.00"
["fourCol"]=>
string(3) "176"
}
[3]=>
array(3) {
["oneCol"]=>
int(4)
["twoCol"]=>
string(17) "Noemi Cruz Campos"
["threeCol"]=>
string(7) "4008.00"
["fourcol"]=>
string(3) "177"
}
}
You have a code error
The line
$info['fourCol'] =$acreditado['id_acreditado'];
Should be
$tmp['fourCol'] =$acreditado['id_acreditado'];
So the code show be: (With the added $tmp array reset)
$j=1;
foreach($acreditados as $acreditado){
$tmp = array();
$tmp['oneCol'] = $j;
$tmp['twoCol'] = $acreditado['acreditado'];
$tmp['threeCol'] = $acreditado['cantidad'];
$tmp['fourCol'] =$acreditado['id_acreditado'];
$info[] = $tmp;
$j++;
}