PHP stdClass Object - foreach - php

I'm using an API to get domain DNS details. The results printed show like this:
stdClass Object
(
[test.co.uk] => stdClass Object
(
[records] => Array
(
[0] => stdClass Object
(
[mname] => ns1.test.com.
[rname] => hostmaster.test.com.
[serial] => 12345678
[refresh] => 1800
[retry] => 900
[expire] => 1209600
[minimum-ttl] => 300
[ref] =>
[host] => test.co.uk
[type] => SOA
)
[1] => stdClass Object
(
[target] => ns1.test.com
[ref] =>
[host] => test.co.uk
[type] => NS
)
[2] => stdClass Object
(
[target] => ns2.test.com
[ref] =>
[host] => test.co.uk
[type] => NS
)
How can I turn each of the records ([0], [1], [2] etc) into variables? I am wanting to show them in a table
I have tried the below, but no result is shown
$Test=$PackageDNSInfo->test.co.uk->records[0]->mname;
echo $Test;

Just use foreach on it, just like you would on an array:
foreach ($PackageDNSInfo as $url => $data) {
foreach ($data->records as $record) {
$type = $record->type;
$host = $record->host;
// etc.
}
}
If you want to access a given URL's data directly, use curly braces:
echo $PackageDNSInfo->{'test.co.uk'}->records[0]->mname;

Related

How to get value out of this...?

Can someone explain me how to get data out of this...like if I just want subject, description..etc...
stdClass Object
(
[tickets] => Array
(
[0] => stdClass Object
(
[url] => https://codemymobilecom.zendesk.com/api/v2/tickets/1.json
[id] => 1
[external_id] =>
[via] => stdClass Object
(
[channel] => sample_ticket
[source] => stdClass Object
(
[from] => stdClass Object
(
)
[to] => stdClass Object
(
)
[rel] =>
)
)
[created_at] => 2015-04-22T08:30:29Z
[updated_at] => 2015-05-19T06:01:22Z
[type] => incident
[subject] => This is a sample ticket requested and submitted by you
[raw_subject] => This is a sample ticket requested and submitted by you
[description] => This is the first comment. Feel free to delete this sample ticket.
[priority] => high
[status] => closed
[recipient] =>
[requester_id] => 794599791
[submitter_id] => 794599791
[assignee_id] => 794599791
[organization_id] => 39742491
[group_id] => 24344491
[collaborator_ids] => Array
(
)
[forum_topic_id] =>
[problem_id] =>
[has_incidents] =>
[due_at] =>
[tags] => Array
(
[0] => sample
[1] => zendesk
)
[custom_fields] => Array
(
)
[satisfaction_rating] =>
[sharing_agreement_ids] => Array
(
)
[fields] => Array
(
)
[followup_ids] => Array
(
)
[brand_id] => 565681
)
[1] => stdClass Object
(
[url] => https://codemymobilecom.zendesk.com/api/v2/tickets/10.json
[id] => 10 //multiple object like [0]...
Thanks...Any help would be great..
When you need to access to array's key, use []. When you have object, use ->.
echo $obj->tickets[0]->subject; // returns first subject
echo $obj->tickets[0]->description; // returns first description
You can put it into foreach loop, of course to gain all subjects, etc.
It's STD object so use properties
$obj->tickets[0]->subject
$obj->tickets[0]->description
You can obviously loop tickets
foreach($obj->tickets as $ticket)
{
echo $ticket->subject;
echo $ticket->description
}
this is an std object.to get subject and description follow this
$obj->tickets[0]->subject;
$obj->tickets[0]->description;
if you feel better in array just make it array using this code
$array = get_object_vars($obj);

Access data in an array

What I want to do is read the information and be able to get the data of each game, so which teams played, who won and etc. I've tried everything but can't seem to do this.
This is my data structure:
stdClass Object (
[_links] => Array (
[0] => stdClass Object (
[self] => http://api.football-data.org/alpha/soccerseasons/354/fixtures
)
[1] => stdClass Object (
[soccerseason] => http://api.football-data.org/alpha/soccerseasons/354
)
)
[count] => 10
[fixtures] => Array (
[0] => stdClass Object (
[_links] => stdClass Object (
[self] => stdClass Object (
[href] => http://api.football-data.org/alpha/fixtures/137842
)
[soccerseason] => stdClass Object (
[href] => http://api.football-data.org/alpha/soccerseasons/354
)
[homeTeam] => stdClass Object (
[href] => http://api.football-data.org/alpha/teams/338
)
[awayTeam] => stdClass Object (
[href] => http://api.football-data.org/alpha/teams/70
)
)
[date] => 2015-01-17T15:00:00Z
[status] => FINISHED
[matchday] => 22
[homeTeamName] => Leicester City
[awayTeamName] => Stoke City FC
[result] => stdClass Object (
[goalsHomeTeam] => 0
[goalsAwayTeam] => 1
)
)
[1] => stdClass Object (
[_links] => stdClass Object (
[self] => stdClass Object (
[href] => http://api.football-data.org/alpha/fixtures/136840
)
[soccerseason] => stdClass Object (
[href] => http://api.football-data.org/alpha/soccerseasons/354
)
[homeTeam] => stdClass Object (
[href] => http://api.football-data.org/alpha/teams/72
)
[awayTeam] => stdClass Object (
[href] => http://api.football-data.org/alpha/teams/61
)
)
[date] => 2015-01-17T15:00:00Z
[status] => FINISHED
[matchday] => 22
[homeTeamName] => Swansea City
[awayTeamName] => Chelsea FC
[result] => stdClass Object (
[goalsHomeTeam] => 0
[goalsAwayTeam] => 5
)
)
)
)
For example: I'd like to know what the value for "homeTeamName","awayTeamName", "goalsHomeTeam", "goalsAwayTeam" ...
Its hard to tell because you didnt post the code you are using but i suspect you are confusing the structure type. What you posted is actually an object - an instance of stdClass and the items that look like array elements are in fact properties on that object. So you need to use the appropriate access method of ->propertyName as opposed to ['propertyName']:
// YES
echo $data->fixtures[0]->homeTeamName;
// NO
echo $data['fixtures'][0]['homeTeamName'];
So to get the data for each game (or what i presume to be the "game" items) you would do:
foreach ($data->fixtures as $game) {
echo $game->homeTeamName;
echo $game->awayTeamName;
echo $game->result->goalsHomeTeam;
echo $game->result->goalsAwayTeam;
// etc..
}
It also seems like this is the return value from an API parsed with json_decode(). If that is the case you can actually make it an array all the way through by passing true as the second argument to json_decode():
$data = json_decode($response, true);
foreach ($data['fixtures'] as $game) {
echo $game['homeTeamName'];
echo $game['awayTeamName'];
echo $game['result']['goalsHomeTeam'];
echo $game['result']['goalsAwayTeam'];
// etc..
}

HubSpot api json decode

I am trying to parse some data out of the Hubspot API response. The response looks like this json_decoded:
stdClass Object(
[addedAt] => 1411052909604
[vid] => 24
[canonical-vid] => 24
[merged-vids] => Array
(
)
[portal-id] => XXXXX
[is-contact] => 1
[profile-token] => AO_T-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[profile-url] => https://app.hubspot.com/contacts/XXXXX/lists/public/contact/_AO_T-XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[properties] => stdClass Object
(
[lastname] => stdClass Object
(
[value] => testtt
)
[firstname] => stdClass Object
(
[value] => test
)
[lastmodifieddate] => stdClass Object
(
[value] => 1411052906670
)
)
[form-submissions] => Array
(
[0] => stdClass Object
(
[conversion-id] => 85d24dd2-9ee9-4d47-b8f3-3035acbd8f3b
[timestamp] => 1411052834097
[form-id] => fb16efd9-23cc-4511-889c-204fc8b41dba
[portal-id] => 401824
[page-url] => http://wbl-1.hs-sites.com/test
[canonical-url] => http://wbl-1.hs-sites.com/test
[content-type] => landing-page
[page-title] => test
[page-id] => 1570433242
[title] => Default Form (Sample)
[first-visit-url] => http://wbl-1.hs-sites.com/test
[first-visit-timestamp] => 1411052722970
[meta-data] => Array
(
)
)
)
[list-memberships] => Array
(
)
[identity-profiles] => Array
(
[0] => stdClass Object
(
[vid] => 24
[identities] => Array
(
[0] => stdClass Object
(
[type] => EMAIL
[value] => test#user.com
[timestamp] => 1411052834097
)
[1] => stdClass Object
(
[type] => LEAD_GUID
[value] => 0b6acf21-6cee-4c7b-b664-e65c11ee2d8e
[timestamp] => 1411052834201
)
)
)
)
[merge-audits] => Array
(
)
)
I'm looking specifically to try to dig out an email inside the indentities-profile area.
I've tried to do the following:
echo $results->contacts[0]->identity-profiles;
But it just gives me a value of 0
Then I try to go further into the array by doing:
echo $results->contacts[0]->identity-profiles[0];
But at that point - I get a parse error:
Parse error: syntax error, unexpected '['
What am I doing wrong? And how can I dig all the way down to
identity-profiles[0]->identities->[0]->value
which should equal: test#user.com
What am I missing?
As mentioned in the comment I would suggest to decode the JSON to an associative array by passing true as second parameter to json_decode. Example: json_decode($data, true) Than you could access your identity profiles by:
$results['contacts'][0]['identitiy-profiles']
If you still want to get the results as an object you have to access the properties the following way because they contain a -:
$results->contacts[0]->{'identity-profiles'}

Foreach loop with mixed stdClass objects and arrays

Here I have an output from a website using Soap
stdClass Object
(
[page] => 0
[items] => 3
[total] => 3
[saleItems] => stdClass Object
(
[saleItem] => Array
(
[0] => stdClass Object
(
[reviewState] => open
[trackingDate] => 2011-11-03T01:06:43.547+01:00
[modifiedDate] => 2011-11-03T01:06:43.677+01:00
[clickDate] => 2011-10-30T22:57:57.383+01:00
[adspace] => stdClass Object
(
[_] => Beslist.nl [id] => 1437603
)
[admedium] => stdClass Object
(
[_] => 001. Program logo
[id] => 535098
)
[program] => stdClass Object
(
[_] => Zavvi NL
[id] => 8991
)
[clickId] => 1565847253976339456
[clickInId] => 0
[amount] => 40.45
[commission] => 2.83
[currency] => EUR
[gpps] => stdClass Object
(
[gpp] => Array
(
[0] => stdClass Object
(
[_] => shoplink
[id] => zpar0
)
)
)
[trackingCategory] => stdClass Object
(
[_] => Default
[id] => 45181
)
[id] => 46a4f84a-ba9a-45b3-af86-da5f3ec29648
)
)
)
)
I want to have the data (with a foreach loop) from program, commission and gpp->_. I can get the data from program and commission like this:
foreach ($sales->saleItems->saleItem as $sale) {
$programma = $sale->program->_;
$commissie = $sale->commission;
}
Works like a charm. However I can't get the data from the gpp->_ (want to have shoplink as result). I currently have:
foreach ($sales->saleItems->saleItem->gpps->gpp as $tracking) {
echo $tracking->_;
}
I get the error "Trying to get property of non-object". I've tried lots if variations and can't get it to work. Think I'm really close. Anyone has a solution?
This should work
foreach ($sales->saleItems->saleItem as $sale) {
foreach($sale->gpps->gpp as $tracking) {
echo $tracking->_;
}
As saleItem is an array, you won't be able to use chaining on it.

Use a foreach loop to access the array within this queryresult object?

A print_r of my object ($results) returns the following:
QueryResult Object
( [queryLocator] => [done] => 1 [records] =>
Array ( [0] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0167 [Partner_Research_Name__c] => MM Sample Organization-TBR Partner 2011 [Id] => a0V80000003FwjjEAC ) [Id] => a0V80000003FwjjEAC )
[1] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0170 [Partner_Research_Name__c] => Kansas City, Missouri Public Schools-TBR Partner 2011 [Id] => a0V80000003Fxf9EAC ) [Id] => a0V80000003Fxf9EAC )
[2] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0169 [Partner_Research_Name__c] => Newark Public Schools-TBR Partner 2011 [Id] => a0V80000003FxQ2EAK ) [Id] => a0V80000003FxQ2EAK )
[3] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0168 [Partner_Research_Name__c] => Breakthrough Charter Schools-TBR Partner 2011 [Id] => a0V80000003FxPxEAK ) [Id] => a0V80000003FxPxEAK )
[4] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0004 [Partner_Research_Name__c] => KIPP, San Antonio-TBR Partner 2011 [Id] => a0V80000003FrBUEA0 ) [Id] => a0V80000003FrBUEA0 )
[5] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0003 [Partner_Research_Name__c] => KIPP, Chicago - Gary-TBR Partner 2011 [Id] => a0V80000003FrB5EAK ) [Id] => a0V80000003FrB5EAK )
[6] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0023 [Partner_Research_Name__c] => Harlem Village Academies-TBR Partner 2011 [Id] => a0V80000003FrEOEA0 ) [Id] => a0V80000003FrEOEA0 ) ) [size] => 7 )
I want to use a loop similar to the what is shown below to display a series of results however the foreach statement is incorrect.
foreach ($results as $result)
{
$id = $result[fields][Id];
$name = $result[fields][Partner_Research_Name__c];
$url = $result[fields][Partner_Research_URL__c];
$html .= "<tr><td>$id</td><td>$name</td><td>$url</td></tr>";
}
What changes do I need to make to the foreach statement to get my code back on track?
What helps me in problems like this is to try to print inside the foreach loop. For example, you can do a var_dump of each $result and see what that structure is, and it could help determine how to proceed.
Here is how I eventually did it, thanks for the help provided by contributors.
foreach ($results->records as $result)
{
$id = $result->fields->Id;
$name = $result->fields->Partner_Research_Name__c;
$url = $result->fields->Partner_Research_URL__c;
$html .= "<tr><td>$id</td><td>$name</td><td>$url</td></tr>";
}
$results is the name of the QueryResults object, I don't know what the real name in your code is.
foreach ($results->records as $result)
{
$id = $result->Id;
$name = $result->fields->Partner_Research_Name__c;
$url = $result->fields->Partner_Research_URL__c;
$html .= "<tr><td>$id</td><td>$name</td><td>$url</td></tr>";
}
If I'm reading your sample correctly, your items are std objects instead of arrays. You may need to reference them like this:
foreach ($results as $result)
{
$id = $result->fields->Id;
$name = $result->fields->Partner_Research_Name__c;
$url = $result->fields->Partner_Research_URL__c;
$html .= "<tr><td>$id</td><td>$name</td><td>$url</td></tr>";
}
Your print_r sample above is difficult to read. If you could provide it with the indentation it would be helpful.

Categories