I'm totally newbie what comes to programming so i'm not even quite sure if my terms are right but i would like to get some hints and tips what is best practice to loop through JSON object? Let's say i want all game names from following JSON print_r output.
stdClass Object
(
[_total] => 555
[_links] => stdClass Object
(
[self] => https://api.twitch.tv/kraken/games/top?limit=2&offset=0
[next] => https://api.twitch.tv/kraken/games/top?limit=2&offset=2
)
[top] => Array
(
[0] => stdClass Object
(
[viewers] => 86386
[channels] => 1159
[game] => stdClass Object
(
[name] => League of Legends
[_id] => 21779
[giantbomb_id] => 24024
[box] => stdClass Object
(
[template] => http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-{width}x{height}.jpg
[small] => http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-52x72.jpg
[medium] => http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-136x190.jpg
[large] => http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-272x380.jpg
)
[logo] => stdClass Object
(
[template] => http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-{width}x{height}.jpg
[small] => http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-60x36.jpg
[medium] => http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-120x72.jpg
[large] => http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-240x144.jpg
)
[_links] => stdClass Object
(
)
)
)
[1] => stdClass Object
(
[viewers] => 17288
[channels] => 162
[game] => stdClass Object
(
[name] => Hearthstone: Heroes of Warcraft
[_id] => 138585
[giantbomb_id] => 42033
[box] => stdClass Object
(
[template] => http://static-cdn.jtvnw.net/ttv-boxart/Hearthstone%3A%20Heroes%20of%20Warcraft-{width}x{height}.jpg
[small] => http://static-cdn.jtvnw.net/ttv-boxart/Hearthstone%3A%20Heroes%20of%20Warcraft-52x72.jpg
[medium] => http://static-cdn.jtvnw.net/ttv-boxart/Hearthstone%3A%20Heroes%20of%20Warcraft-136x190.jpg
[large] => http://static-cdn.jtvnw.net/ttv-boxart/Hearthstone%3A%20Heroes%20of%20Warcraft-272x380.jpg
)
[logo] => stdClass Object
(
[template] => http://static-cdn.jtvnw.net/ttv-logoart/Hearthstone%3A%20Heroes%20of%20Warcraft-{width}x{height}.jpg
[small] => http://static-cdn.jtvnw.net/ttv-logoart/Hearthstone%3A%20Heroes%20of%20Warcraft-60x36.jpg
[medium] => http://static-cdn.jtvnw.net/ttv-logoart/Hearthstone%3A%20Heroes%20of%20Warcraft-120x72.jpg
[large] => http://static-cdn.jtvnw.net/ttv-logoart/Hearthstone%3A%20Heroes%20of%20Warcraft-240x144.jpg
)
[_links] => stdClass Object
(
)
)
)
)
)
I can access single line (not sure if this is best practise either):
$OBJ->method()->top[0]->game->name;
But i'm more than clueless how to loop through all game names.
Any help much appreciated!
The "name"s are accessed using $OBJ->top[0]->game->name etc... So just foreach over the "top" array:
foreach($OBJ->top as $object) {
echo $object->game->name;
}
Create an empty array, loop the objects top array and fill your empty array:
$allgames=array();
foreach($OBJ->method()->top as $ob){
$allgames[] = $ob->game->name;
}
When you load your JSON string into PHP you can use:
json_decode($string_of_json, true);
The true flag will load it into an array you can loop through using, for example, foreach.
Related
stdClass Object
(
[kind] => youtube#channelListResponse
[etag] => "Fznwjl6JEQdo1MGvHOGaz_YanRU/AwtUznqL5IWQ_LGBo6hPktZlciI"
[pageInfo] => stdClass Object
(
[totalResults] => 1
[resultsPerPage] => 1
)
[items] => Array
(
[0] => stdClass Object
(
[kind] => youtube#channel
[etag] => "Fznwjl6JEQdo1MGvHOGaz_YanRU/89K2C_1kE4Ku8LvtzDJQlV5_e2I"
[id] => UC-lHJZR3Gqxm24_Vd_AJ5Yw
[snippet] => stdClass Object
(
[title] => PewDiePie
[description] => I make videos.
[publishedAt] => 2010-04-29T10:54:00.000Z
[thumbnails] => stdClass Object
(
[default] => stdClass Object
(
[url] => https://yt3.ggpht.com/a/AGF-l79FVckie4j9WT-4cEW6iu3gPd4GivQf_XNSWg=s88-c-k-c0xffffffff-no-rj-mo
[width] => 88
[height] => 88
)
[medium] => stdClass Object
(
[url] => https://yt3.ggpht.com/a/AGF-l79FVckie4j9WT-4cEW6iu3gPd4GivQf_XNSWg=s240-c-k-c0xffffffff-no-rj-mo
[width] => 240
[height] => 240
)
[high] => stdClass Object
(
[url] => https://yt3.ggpht.com/a/AGF-l79FVckie4j9WT-4cEW6iu3gPd4GivQf_XNSWg=s800-c-k-c0xffffffff-no-rj-mo
[width] => 800
[height] => 800
)
)
[localized] => stdClass Object
(
[title] => PewDiePie
[description] => I make videos.
)
[country] => US
)
)
)
)
How can I get the value of (e.g. url of the high stdClass object)?
Should I have to create first an array or can I access directly to this value? I don't know how to get the value of an object which is property from another object.
Thank in advance!
In your case:
echo $myObject->items[0]->id; //Outputs UC-lHJZR3Gqxm24_Vd_AJ5Yw
//or
echo $myObject->items[0]->snippet->thumbnails->default->url; //Outputs: https://yt3.ggpht.com/a/AGF-l79FVckie4j9WT-4cEW6iu3gPd4GivQf_XNSWg=s88-c-k-c0xffffffff-no-rj-mo
Object attributes must be refered with arrow (->) and array elements with brackets ([]).
items is an array, as you can see in your output.
In this example, I'm reading items from the main object ($myObject), the first index from items array (0), then it's property id;
Something strange happens during saving into an array inside a foreach.
I'm getting unexpected results so I make this part of code for debuging.
$search_case = new stdClass();
$same_case = array ();
print_r($families);
foreach ($families as $key => $family) {
echo "<br/><br/>";
$same_case['aaaaa'.$key] = $this->search_benef->search_case($family, $search_case);
print_r($same_case);
}
The problem is that when I get function's results for $key = 1 it also change the data in table for $key = 0. To chek that I created aaaaa0 and aaaaa1 names for my table.
Bellow is print_r results:
First print_r I have 2 objects, IBRAHIM and MOHAMMAD
stdClass Object ( [0] => stdClass Object ( [focal] => stdClass Object ( [name] => RENAS [surname] => IBRAHIM [case_number] => 36343 ) [case] => 36343 ) [1] => stdClass Object ( [focal] => stdClass Object ( [name] => AZAD [surname] => MOHAMMAD [case_number] => 54944 ) [case] => 54944 ) )
Second print_r inside foreach loop, in the first loop I get results for IBRAHIM and in the second loop Ι get results for MOHAMMAD, but it removes results for IBRAHIM in aaaaa0.
Array ( [aaaaa0] => stdClass Object ( [old] => stdClass Object ( [hotel] => stdClass Object ( [focal] => stdClass Object ( [id] => 26 [name] => RENAS [surname] => IBRAHIM ) ) ) ) )
Array ( [aaaaa0] => stdClass Object ( [old] => stdClass Object ( [hotel] => stdClass Object ( [focal] => stdClass Object ( [id] => 22 [name] => AZAD [surname] => MOHAMMAD ) ) [athens] => stdClass Object ( [focal] => stdClass Object ( [id] => 24 [name] => AZAD [surname] => MOHAMMAD ) ) ) ) [aaaaa1] => stdClass Object ( [old] => stdClass Object ( [hotel] => stdClass Object ( [focal] => stdClass Object ( [id] => 22 [name] => AZAD [surname] => MOHAMMAD ) ) [athens] => stdClass Object ( [focal] => stdClass Object ( [id] => 24 [name] => AZAD [surname] => MOHAMMAD ) ) ) ) )
Can you explain why this happens? Which is the error?
-- EDIT --
I change my code and I remove 'search_case' function, but I face the same problem. The content changes. Below I paste my new code without the function.
$aaaaa = array ();
foreach ($families as $key => $family) {
echo "<br/><br/>Family->Case Number: " . $family->case;
$db_lists = array('athens' => 'athensDB', 'hotel' => 'hotelDB');
foreach ($db_lists as $key => $db_list) {
$remoteDB = $this->ci->load->database($key, TRUE);
$query = 'SELECT tb.id as id, name, surname FROM beneficiaries tb
left join _map_family_status tmfs ON tmfs.id=tb.family_status
where asylum_case = ' . $family->case . ' AND family_status = 1 GROUP BY asylum_case';
$focal = $remoteDB->query($query)->row();
if ($focal) {
$search_case->old->$key->focal = $focal;
}
}
echo '<br/> '.$family->case.' <br/> ';
$aaaaa[$family->case] = $search_case;
echo "<pre>";
print_r($aaaaa);
echo "</pre>";
}
return $aaaaa;
But the results are dissapointing. Array[36343] content changes with the second loop's content. Below I paste "print_r" results for the two loops.
Family->Case Number: 36343
Array (
[36343] => stdClass Object (
[old] => stdClass Object (
[hotel] => stdClass Object (
[focal] => stdClass Object (
[id] => 26
[name] => RENAS
[surname] => IBRAHIM
)
)
)
)
)
Family->Case Number: 54944
Array (
[36343] => stdClass Object (
[old] => stdClass Object (
[hotel] => stdClass Object (
[focal] => stdClass Object (
[id] => 22
[name] => AZAD
[surname] => MOHAMMAD
)
)
[athens] => stdClass Object (
[focal] => stdClass Object (
[id] => 24
[name] => AZAD
[surname] => MOHAMMAD
)
)
)
)
[54944] => stdClass Object (
[old] => stdClass Object (
[hotel] => stdClass Object (
[focal] => stdClass Object (
[id] => 22
[name] => AZAD
[surname] => MOHAMMAD
)
)
[athens] => stdClass Object (
[focal] => stdClass Object (
[id] => 24
[name] => AZAD
[surname] => MOHAMMAD
)
)
)
)
)
Below is an example of the array I have compiled so far. I am generating the array from a facebook graph api call and want to remove the Array wrapping each object so I just have one list under the data Array. Preferably I need a dynamic solution as their could be more than one [0] => Array, [1] => Array and so on.... in each API request.
stdClass Object
(
[data] => Array
(
[0] => Array
(
[0] => stdClass Object
(
[id] => 21744379694_10154626935079695
[created_time] => 2016-10-16T06:29:28+0000
[from] => stdClass Object
(
[name] => Tony Hawk
[id] => 21744379694
)
)
)
[1] => Array
(
[0] => stdClass Object
(
[id] => 50043151918_10154176205946919
[created_time] => 2016-10-15T20:04:22+0000
[from] => stdClass Object
(
[name] => GoPro
[id] => 50043151918
)
)
)
)
)
I would like the array to look like this ultimately. What is the best approach here?
stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[id] => 21744379694_10154626935079695
[created_time] => 2016-10-16T06:29:28+0000
[from] => stdClass Object
(
[name] => Tony Hawk
[id] => 21744379694
)
)
[1] => stdClass Object
(
[id] => 50043151918_10154176205946919
[created_time] => 2016-10-15T20:04:22+0000
[from] => stdClass Object
(
[name] => GoPro
[id] => 50043151918
)
)
)
)
Assuming your data is stored in a variable $data, you can do this:
foreach($data->data as &$el) {
$el = $el[0];
}
Now the wrapping arrays have been removed.
See it run on eval.in
You can use this:
if ($data && $data->data){ $new = array_values($data->data); }
I am using the php library from here and I have a small problem.
To get some info for a invoice 118868, I can do this
<?php
$invoice_id=118868;
$invoice=$freshbooks->invoiceGet($invoice_id);
print_r($invoice);
?>
This is the output for print_r
Class Object ( [#attributes] => stdClass Object ( [status] => ok ) [invoice] => stdClass Object ( [invoice_id] => 00000219023 [estimate_id] => stdClass Object ( ) [number] => 8822 [client_id] => 83 [contacts] => stdClass Object ( [contact] => stdClass Object ( [contact_id] => 0 ) ) [recurring_id] => stdClass Object ( ) [organization] => Jimmy Thwart [first_name] => stdClass Object ( ) [last_name] => stdClass Object ( ) [p_street1] => stdClass Object ( ) [p_street2] => stdClass Object ( ) [p_city] => stdClass Object ( ) [p_state] => stdClass Object ( ) [p_country] => stdClass Object ( ) [p_code] => stdClass Object ( ) [po_number] => 10002 [status] => sent [amount] => 16.90 [amount_outstanding] => 16.90 [paid] => 0.00 [date] => 2013-01-31 00:00:00 [notes] => stdClass Object ( ) [terms] => Your slot can only be secured upon payment. Any reservation made before payment will only be guaranteed for 2 days. [discount] => 0 [url] => https://example.freshbooks.com/view/vgPb2TNGCR7n8JV [auth_url] => https://example.freshbooks.com/invoices/219023 [links] => stdClass Object ( [client_view] => https://example.freshbooks.com/view/vgPb2TNGCR7n8JV [view] => https://example.freshbooks.com/invoices/219023 [edit] => https://example.freshbooks.com/invoices/219023/edit ) [return_uri] => stdClass Object ( ) [updated] => 2013-01-31 02:25:13 [currency_code] => SGD [language] => en [vat_name] => stdClass Object ( ) [vat_number] => stdClass Object ( ) [folder] => active [staff_id] => 1 [lines] => stdClass Object ( [line] => stdClass Object ( [line_id] => 1 [name] => Lady 1pax [description] => Services (1 pax) [unit_cost] => 16.90 [quantity] => 1 [amount] => 16.90 [tax1_name] => stdClass Object ( ) [tax2_name] => stdClass Object ( ) [tax1_percent] => 0 [tax2_percent] => 0 [compound_tax] => 0 [type] => Item ) ) [gateways] => stdClass Object ( [gateway] => stdClass Object ( [name] => PayPal ) ) ) )
I hope the output can only be the URL and not this whole chunk of code.
Output wanted:
https://example.freshbooks.com/view/vgPb2TNGCR7n8JV
However, it lists all information of the invoice. How do I get only the invoice URL?
Well, using the output of your code, you can see what the elements of $invoice are. Use the one that you need.
Invoice is an instane of a class. You should treat it like all instances.
To get URL value you can do the next:
<?php
$invoice_id=118868;
$invoice=$freshbooks->invoiceGet($invoice_id);
print $invoice->url;
?>
or there's another possible value you're looking for:
<?php
$invoice_id=118868;
$invoice=$freshbooks->invoiceGet($invoice_id);
print $invoice->links->client_view;
?>
According to official API doc <url> is not supporting any more, so you should use <links> "element to provide your customers with direct links to the invoice for editing, viewing by the client and viewing by an administrator."
i decoded the json data using php json_decode
here is my output with print_r
stdClass Object (
[uv] => stdClass Object (
[current] => stdClass Object (
[class_name] => delta-positive
[display] => 668 [value] => 668 )
[yoy_perc] => stdClass Object (
[class_name] => delta-negative
[display] => -21.23%
[value] => -21.23 )
[mom_perc] => stdClass Object (
[class_name] => delta-negative
[display] => -8.87% [value] => -8.87 )
[yoy] => stdClass Object (
[class_name] => delta-negative
[display] => -180 [value] => -180 )
[mom] => stdClass Object (
[class_name] => delta-negative
[display] => -65
[value] => -65 ) )
[page_title] => example.com 668 UVs for December 2012 | Compete
[rank] => stdClass Object (
[current] => stdClass Object (
[class_name] => delta-positive
[display] => 1,398,481
[value] => 1398481 ) [yoy] => stdClass Object ( [class_name] => delta-negative [display] => -187,667 [value] => -187667 ) [last_month] => stdClass Object ( [class_name] => delta-positive [display] => 1,246,200 [value] => 1246200 ) [mom] => stdClass Object ( [class_name] => delta-negative [display] => -152,281 [value] => -152281 ) [last_year] => stdClass Object ( [class_name] => delta-positive [display] => 1,210,814 [value] => 1210814 ) ) )
i need to get
[display] => 668 [value] => 668 )
668
of this i tried using foreach but no luck. anyone know how to do that using php an efficient way i mean without a loop
thank you
$obj->uv->current->value
Try this:
object->uv->current->display;
object->uv->current->value;
or use json_decode PHP function.
Try using json_decode() with the second parameter set to get it to output an array rather than an object structure.
`$outputArray = json_decode($inputString, true);
That will allow you to loop through it using foreach().
If you don't feel comfortable with objects you can simply put a "true" to the second parameter of json_decode().
json_decode($jsondata, true);
So you can access them as an array.
use json_decode($json, true) to get an associative arrays.
http://www.php.net/manual/en/function.json-decode.php