I'm trying to loop through a stdClass object ($args) which is formatted as shown below. The goal is to loop through each of the sObjects, JSON encode the values and send it to an endpoint.
I figured this would do the trick:
$object = $args->Notification->sObject;
foreach($object as $item) { JSON_ENCODE AND SEND TO PASTEB.IN }
Except I just get a 'null' response.
If I do this:
$object = $args->Notification;
foreach($object as $item) {
$sf = $item->sObject;"
// JSON_ENCODE $sf and send to PASETBI.N
}
... I get the result of only one result posted to pasteb.in, not all of them (e.g 3 separate results as should be the case in the below table).
I've had a look at answers to other questions regarding looping through arrays/sObjects but I can't get it right. Any help would be much appreciated!
Print_r'ing this object shows this format:
stdClass Object
(
[OrganizationId] => XXXXXXXXXXXXXXXX
[ActionId] => XXXXXXXXXXXXXXXX
[SessionId] =>
[EnterpriseUrl] => https://darlston-dev-ed.my.salesforce.com/services/Soap/c/33.0/XXXXXXXXXXXXXXXX
[PartnerUrl] => https://darlston-dev-ed.my.salesforce.com/services/Soap/u/33.0/XXXXXXXXXXXXXXXX
[Notification] => Array
(
[0] => stdClass Object
(
[Id] => XXXXXXXXXXXXXXXX
[sObject] => stdClass Object
(
[Id] => XXXXXXXXXXXXXXXX
[Contact_Email__c] => test#testemail.com
)
)
[1] => stdClass Object
(
[Id] => XXXXXXXXXXXXXXXX
[sObject] => stdClass Object
(
[Id] => XXXXXXXXXXXXXXXX
[Contact_Email__c] => test#testemail.com
)
)
[2] => stdClass Object
(
[Id] => XXXXXXXXXXXXXXXX
[sObject] => stdClass Object
(
[Id] => XXXXXXXXXXXXXXXX
[Contact_Email__c] => test#testemail.com
)
)
)
)
Can you try this?
foreach($args->Notification as $item)
{
echo $item->Id."<br>";
echo $item->sObject->Id."<br>";
echo $item->sObject->Contact_Email__c."<br>";
}
Related
I have a XML format file which has objects inside objects. This is how I get values of first object, which works fine for me:
$soapclient = new SoapClient('http://anywb/book.asmx?WSDL');
$params = array('ISBN' => "1111");
$response = $soapclient->GetBookByISBN($params);
//This will give me the value "Success"
$result = $response->GetBookByISBNResult->ResponseText;
Now my question is how to access the object which is inside object. For example, how to get "BookID" which is 4 and how to get value of "Type" which is 1?
Any suggestion would be appreciated. Here is the object:
stdClass Object
(
[GetBookByISBN] => stdClass Object
(
[ResponseText] => Success
[SearchResult] => stdClass Object
(
[Search] => Array
(
[0] => stdClass Object
(
[Date] => 2015-10-20
[BookID] => 4
[Discription] => stdClass Object
(
[Type] => 1
)
[Probability] => stdClass Object
(
[Kids] =>
[Adult] => 00
)
)
[1] => stdClass Object
(
[Date] => 2016-11-15
[BookID] => 5
[Discription] => stdClass Object
(
[Type] => 2
)
[Probability] => stdClass Object
(
[Kids] =>
[Adult] => 00
)
)
))))
You do it like this:
$response->GetBookByISBN->SearchResult->Search[0]->BookID;
$response->GetBookByISBN->SearchResult->Search[0]->Discription->Type;
If you would like to have an array containing all book ids you would have something like this:
$BookIDs = array();
foreach($response->GetBookByISBN->SearchResult->Search as $key => $value) {
$BookIDs[$key] = $value->BookID;
}
Or if you would like to be able to modify the values in $response using the $BookIDs (as an example) iot would look like this:
$BookIDs = array();
foreach($response->GetBookByISBN->SearchResult->Search as $key => $value) {
$BookIDs[$key] = &$value->BookID;
}
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..
}
I try to take from bing api related results and write script, who take me with print_r($value) this
Array
(
[0] => stdClass Object
(
[__metadata] => stdClass Object
(
[uri] => https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/RelatedSearch?Market='en-US'&Query='car'&$skip=0&$top=1
[type] => RelatedSearchResult
)
[ID] => 8bbe5043-f85b-41b3-b044-3649628fc5cf
[Title] => Cars Games
[BingUrl] => http://www.bing.com/search?q=Cars+Games
)
[1] => stdClass Object
(
[__metadata] => stdClass Object
(
[uri] => https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/RelatedSearch?Market='en-US'&Query='car'&$skip=1&$top=1
[type] => RelatedSearchResult
)
[ID] => b9df73ab-c410-4480-b172-2719643d1120
[Title] => Car Pictures
[BingUrl] => http://www.bing.com/search?q=Car+Pictures
)
)
How i can take [Title] and [BingUrl] from this array ? Thank you.
Use [] to access array elements and use -> to access object members.
In your case, it would be $arr[$index]->Title and $arr[$index]->BingUrl
Most APIs provide results in JSON format, so you may save yourself some headache by using json_decode($api_result, 1) to decode them as an array instead of an object.
If $value is array then you can use this code
foreach ($value as $item)
{
echo $item->Title;
echo $item->BingUrl
}
I have a JSON array that I want to be able to drill down to a lower level and print just that value. The problem occurs when I reach a level that has is indacted as [0] (or [n]). For example I have the following output, and I want to just print the game key for the first league.
This is how I am trying to print it
HtmlSpecialChars(print_r($user->fantasy_content->users[0]->user[1]->games[0]->game[0]->game_key,1))
However I keep getting this error:
Cannot use object of type stdClass as array
When I do it incrementally it seems to fail on this command (so I assume I'm not index correctly):
$user->fantasy_content->users[0]
Here is the output:
stdClass Object
(
[fantasy_content] => stdClass Object
(
[xml:lang] => en-US
[yahoo:uri] => /fantasy/v2/users;use_login=1/games
[users] => stdClass Object
(
[0] => stdClass Object
(
[user] => Array
(
[0] => stdClass Object
(
[guid] => IYEZUHTVBYRLIB3OAQC5WRZPQY
)
[1] => stdClass Object
(
[games] => stdClass Object
(
[0] => stdClass Object
(
[game] => Array
(
[0] => stdClass Object
(
[game_key] => 147
[game_id] => 147
[name] => Baseball
[code] => mlb
[type] => full
[url] => http://baseball.fantasysports.yahoo.com/b1
[season] => 2006
)
)
)
[count] => 1
)
)
)
)
[count] => 1
)
[time] => 52.390813827515ms
[copyright] => Data provided by Yahoo! and STATS, LLC
[refresh_rate] => 60
)
)
For objects you must use the -> syntax and if the key/property name is a number or has other special characters, you will need to use the $object->{'0'} syntax.
The game_key can be retrieved using:
$user->fantasy_content->users->{'0'}->user[1]->games->{'0'}->game[0]->game_key;
You can convert a stdClass object to an array by casting it like so:
<?php
$array = (array) $myObject;
echo json_encode($array);
You can also cast inline:
<?php
echo json_encode((array) $object);
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.