I am new to Objects , and I've been playing with this little object. Now I would like to know how I can acess an specific element of it .
This is the object :
stdClass Object ( [responseHeader] => stdClass Object ( [status] => 0 [QTime] => 0 ) [response] => stdClass Object ( [numFound] => 1 [start] => 0 [docs] => Array ( [0] => stdClass Object ( [schoolname_s] => School of Art [schoolstate_s] => FL [schoolcity_s] => Miami [pk_id] => 111212 [schoolcountry_s] => United States [name_s] => Example ) ) ) )
I would like to print pk_id.
I have tried :
$phpArray->pk_id;
And this was the result:
Notice: Undefined property: stdClass::$pk_id in
You aren't accessing the right variable. What you want is this:
$phpArray->response->docs[0]->pk_id
Related
how get value feild[id] in php code
stdClass Object ( [List_inserted] => Array ( [0] => stdClass Object ( [ID] => 145001 [value] => 40 ) ) [Sucssess] => 1 [ErrorMassage] => OK )
You didn't give us a name of stdClass so I'm assuming it's $stdClass.
$stdClass->List_inserted[0]->ID
Let's break it down;
stdClass Object ( [List_inserted] => Array ( [0] => stdClass Object ( [ID] => 145001 [value] => 40 ) ) [Sucssess] => 1 [ErrorMassage] => OK )
We access objects with a -> and we access arrays with []
The first part tells us it's an object, so it's;
$stdClass->List_inserted
List_inserted is an array thanks to the => Array. We can access this with [0].
$stdClass->List_inserted[0]
Well, List_inserted[0] is an object, thanks too [0] => stdClass Object; and you wanted to access the ID? So we need another ->
$stdClass->List_inserted[0]->ID
please help me for read a parameter from array in php:
when use from print_r for view array , result is:
getUserServicesResponse Object (
[getUserServiceResponse] =>
[return] => stdClass Object (
[isactive] => 1
[lastQnum] => 0
[qnum] => 5
[score] => 0
[service] => stdClass Object (
[countActive] => 73657
[countAll] => 199784
[lastTime] => 2015-12-01T08:38:06.065+03:30
[maxScore] => 33000
[minScore] => 0
[minregdate] => 2014-08-05T15:27:12+04:30
[serviceName] => game
[topNumber] => 09121153321
)
[serviceID] => 12946
)
)
i want print serviceName value from array that is game or print score from array that is 0
thanks
To access a parameter from php object (stdClass or normal)
you need object name with -> and the attribute name which you want to access,
in your case there are many stdClass objects one after another so the correct ways is,
echo $getUserServicesResponse->return->service->serviceName;
output:
game
FYI
the structure for which i have generated the output
stdClass Object
(
[return] => stdClass Object
(
[isactive] => Harry Potter and the Prisoner of Azkaban
[service] => stdClass Object
(
[serviceName] => game
)
)
)
Try this
$object->getUserServiceResponse->service->serviceName;
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 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'}
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);