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
Related
Hi I've asked this before but didn't get a correct answer. I am trying to get the Tracks to print from the stdObject. I've tried many possible answers but nothing worked apart from using:
var_dump("Track Title: " .
$response->Items->Item->Tracks->Disc->Track[0]->_);
But this only works if you know how many tracks there are for certain CD, I am calling 500 ASIN's at once. So basically I would like to parse the Tracks array into a String
[Items] => stdClass Object
(
[Request] => stdClass Object
(
[IsValid] => True
[ItemLookupRequest] => stdClass Object
(
[IdType] => ASIN
[ItemId] => B000002OGL
[ResponseGroup] => Tracks
[VariationPage] => All
)
)
[Item] => stdClass Object
(
[ASIN] => B000002OGL
[Tracks] => stdClass Object
(
[Disc] => stdClass Object
(
[Track] => Array
(
[0] => stdClass Object
(
[_] => Mustang Sally
[Number] => 1
)
[1] => stdClass Object
(
[_] => Take Me To The River
[Number] => 2
)
[2] => stdClass Object
(
[_] => Chain Of Fools
[Number] => 3
)
[3] => stdClass Object
(
[_] => The Dark End Of The Street
[Number] => 4
)
[4] => stdClass Object
(
[_] => Destination: Anywhere
[Number] => 5
)
[5] => stdClass Object
(
[_] => I Can't Stand The Rain
[Number] => 6
)
[6] => stdClass Object
(
[_] => Try A Little Tenderness
[Number] => 7
)
[7] => stdClass Object
(
[_] => Treat Me Right
[Number] => 8
)
[8] => stdClass Object
(
[_] => Do Right Woman Do Right Man
[Number] => 9
)
[9] => stdClass Object
(
[_] => Mr. Pitiful
[Number] => 10
)
[10] => stdClass Object
(
[_] => I Never Loved A Man
[Number] => 11
)
[11] => stdClass Object
(
[_] => In The Midnight Hour
[Number] => 12
)
[12] => stdClass Object
(
[_] => Bye Bye Baby
[Number] => 13
)
[13] => stdClass Object
(
[_] => Slip Away
[Number] => 14
)
)
[Number] => 1
)
)
)
)
)
you can do
foreach($response->Items->Item->Tracks->Disc->Track as $track){
echo "Track Title: ". $track->_;// or track title
}
I'm really confused. Normally i use array push to add new data to a array. But it doesnt work atm. Can someone help me?
Array
(
[0] => stdClass Object
(
[name] => Knijn zijn
[artist] => Bertie Lukano
[url] => http://www.last.fm/music/Bertie+Lukano/_/Knijn+zijn
[streamable] => stdClass Object
(
[text] => 0
[fulltrack] => 0
)
[listeners] => 1
[mbid] =>
)
[1] => stdClass Object
(
[name] => knijnzijn
[artist] => Bertie Lukano
[url] => http://www.last.fm/music/Bertie+Lukano/_/knijnzijn
[streamable] => stdClass Object
(
[text] => 0
[fulltrack] => 0
)
[listeners] => 1
[mbid] =>
)
[2] => stdClass Object
(
[name] => Wij Vieren Feest
[artist] => Bertie Lukano
[url] => http://www.last.fm/music/Bertie+Lukano/_/Wij+Vieren+Feest
[streamable] => stdClass Object
(
[text] => 0
[fulltrack] => 0
)
[listeners] => 1
[mbid] =>
)
[3] => stdClass Object
(
[name] => Hieper de piep ik heb de mexicaanse griep!
[artist] => Bertie Lukano
[url] => http://www.last.fm/music/Bertie+Lukano/_/Hieper+de+piep+ik+heb+de+mexicaanse+griep!
[streamable] => stdClass Object
(
[text] => 0
[fulltrack] => 0
)
[listeners] => 1
[mbid] =>
)
)
So i need to add a new stdClass Object but i cant figure out how.
[4] => stdClass Object
(
[name] => name
[artist] => artist
[url] => url
[streamable] => stdClass Object
(
[text] => 0
[fulltrack] => 0
)
[listeners] => 1
[mbid] =>
)
As you see inside the object need to be another object at [streamable] so i really dont know how to do that so i hope someone can help me.
Thanks
I would avoid at all costs mixing objects within arrays to save yourself the headache later.
Usually this is how they're added:
$oData = new stdClass;
$oData->name = 'Bob';
$oData->email = 'Bob#bob.com';
$aArray = array();
$aArray[] = $oData;
var_dump( $aArray );
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.
This question already has answers here:
Able to see a variable in print_r()'s output, but not sure how to access it in code
(9 answers)
Closed 8 years ago.
Here is a print_r() of my returned object:
Array
(
[0] => stdClass Object
(
[list_id] => 547009977
[list_name] => Master List
[list_type] => email
[member_data] => Array
(
[0] => stdClass Object
(
[name] => work_phone
[value] =>
)
[1] => stdClass Object
(
[name] => city
[value] =>
)
[2] => stdClass Object
(
[name] => restricted_since
[value] =>
)
[3] => stdClass Object
(
[name] => unsub_campaign_id
[value] =>
)
[4] => stdClass Object
(
[name] => title
[value] =>
)
[5] => stdClass Object
(
[name] => comments
[value] =>
)
[6] => stdClass Object
(
[name] => company_name
[value] =>
)
[7] => stdClass Object
(
[name] => Info
[value] =>
)
[8] => stdClass Object
(
[name] => address_hash
[value] =>
)
[9] => stdClass Object
(
[name] => hash
[value] => 2054ee9827
)
[10] => stdClass Object
(
[name] => country
[value] =>
)
[11] => stdClass Object
(
[name] => id
[value] => 45
)
[12] => stdClass Object
(
[name] => gender
[value] =>
)
[13] => stdClass Object
(
[name] => postalcode
[value] =>
)
[14] => stdClass Object
(
[name] => address_1
[value] =>
)
[15] => stdClass Object
(
[name] => create_date
[value] => 2013-07-15T15:31:35+00:00
)
[16] => stdClass Object
(
[name] => optin_status_last_updated
[value] => 2013-07-15T15:31:35+00:00
)
[17] => stdClass Object
(
[name] => Purchased
[value] =>
)
[18] => stdClass Object
(
[name] => marital_status
[value] =>
)
[19] => stdClass Object
(
[name] => optin_status
[value] => null
)
[20] => stdClass Object
(
[name] => last_updated
[value] => 2013-07-15T15:31:35+00:00
)
[21] => stdClass Object
(
[name] => address_2
[value] =>
)
[22] => stdClass Object
(
[name] => home_phone
[value] =>
)
[23] => stdClass Object
(
[name] => fax
[value] =>
)
[24] => stdClass Object
(
[name] => first_name
[value] => CoregTest
)
[25] => stdClass Object
(
[name] => bounce_campaign_id
[value] =>
)
[26] => stdClass Object
(
[name] => Page
[value] =>
)
[27] => stdClass Object
(
[name] => is_cass_certified
[value] => 0
)
[28] => stdClass Object
(
[name] => last_name
[value] =>
)
[29] => stdClass Object
(
[name] => restricted
[value] => 0
)
[30] => stdClass Object
(
[name] => mobile_phone
[value] =>
)
[31] => stdClass Object
(
[name] => email_address
[value] => coregtest#chrisjallen.com
)
[32] => stdClass Object
(
[name] => ip_address
[value] =>
)
[33] => stdClass Object
(
[name] => state
[value] =>
)
)
)
)
All I want to do is access the list_id in my php code. If I put it into a $result var:
$result = $vr->searchListMembers( array(
'session_id' => $sid,
'field_name' => 'email_address',
'field_value' => $email,
'max_records' => 1
) );
I've tried
$result->list_id
and
$result[0]['list_id']
This is seemingly obvious but I'm missing something here.
Try:
$list_id = $result[0]->list_id;
$list_id = $result['list_id'];
however, list_id isn't in your code. So perhaps you meant session_id. Regardless, the syntax is:
array['key']
ChromePHP doesn't seem like a very useful tool. This is because PHP and JavaScript have different data types/data structures, so a JavaScript console output won't tell you about how the object looks in PHP.
In JavaScript, objects and arrays can be accessed using []. In PHP, objects and arrays are accessed with different syntax. PHP arrays use [] and objects use ->.
If you want to debug PHP, I suggest you use var_dump (or print_r), this will show you if you have an array or an object. A JavaScript console will always show object, and is not very useful.
In your case, you need to access your element via:
$result[0]->list_id
Omitting the fact that list_I'd isn't there it really depends on the return type of the searchListMember method you are using. If the returned var is an object you have to use ->list_id if its an array then it really about the structure but it should go like $result['list_id']
Try this!
print $result[0]->list_id;
If you do :
$result = $vr->searchListMembers( array(
'session_id' => $sid,
'field_name' => 'email_address',
'field_value' => $email,
'max_records' => 1
) );
$result is an Array.
So first you have to access the first element: $result[0]
Then, it is a stdClass object so you can access its properties with the -> operator:
$result[0]->list_id
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."