How to read a parameter from an array in PHP? - php

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;

Related

how to get value from this php array [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 7 years ago.
i have this array from json_decode
stdClass Object (
[coord] => stdClass Object (
[lon] => 8.97
[lat] => 51
)
[weather] => Array (
[0] => stdClass Object (
[id] => 804
[main] => Clouds
[description] => overcast clouds
[icon] => 04d
)
)
[base] => stations
[main] => stdClass Object (
[temp] => 281.17
[pressure] => 1014
[humidity] => 87
[temp_min] => 280.93
[temp_max] => 281.48
)
[visibility] => 7000
[wind] => stdClass Object (
[speed] => 3.6
[deg] => 180
)
[clouds] => stdClass Object (
[all] => 90
)
[dt] => 1445499854
[sys] => stdClass Object (
[type] => 1
[id] => 4954
[message] => 0.0056
[country] => DE
[sunrise] => 1445493535
[sunset] => 1445530644
)
[id] => 2906244
[name] => Herbelhausen
[cod] => 200
)
i have get from it the description
$response_a2->weather[0]->description
but wen am trying to get the temp or pressure or humidity or speed i filed
$response_a2->base->temp
You are looking for
$response_a2->main->temp
Thanks to RiggsFolly for beautifying the array.
next time, write your code like this:
echo "<pre">;
print_r($response_a2);
echo "</pre>";
and your array will look beautiful in the browser
That data does not exist in $response_a2->base->temp
Instead us
$response_a2->main->temp
$response_a2->main->presure
$response_a2->main->humidity
etc
What you are showing us is an object, not an array. Objects work differently from arrays. They use the -> operator to reach certain values.
For example, imagine I've got object $obj and I wanna reach value ['some_value'].
echo $obj->some_value;

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);

PHP Access Object

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

PHP Printing JSON value

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);

how to convert Object array to array while single value returning

how to convert Object array to array while single value returning ,
here is the o/p what i am getting while i am getting only one hotel, how can i convert it in a way to access it as a 0th [0] value shown in code. i have problems in accessing when i m getting single hotel in o/p. print_r($result); via foreach()
stdClass Object
(
[cityId] => 000000000020
[checkInDate] => 2013-12-20
[checkOutDate] => 2013-12-21
[customerId] =>
[customerAccountType] => SH01
[customerType] =>
[currency] => INR
[noOfRoomsRequested] => 1
[searchAvailabilityResult] => stdClass Object
(
[hotelId] => IXW1
[hotelName] => Ginger Jamshedpur
)
)
===what i want is ================================
stdClass Object
(
[cityId] => 000000000020
[checkInDate] => 2013-12-20
[checkOutDate] => 2013-12-21
[customerId] =>
[customerAccountType] => SH01
[customerType] =>
[currency] => INR
[noOfRoomsRequested] => 1
[searchAvailabilityResult] => Array
(
[0] => stdClass Object
(
[hotelId] => IXW1
[hotelName] => Ginger Jamshedpur
)
)
)
in case of single value
$result->searchAvailabilityResult = array($result->searchAvailabilityResult);
Many thanks to #Maciej Sz
$result->searchAvailabilityResult = get_object_vars($result->searchAvailabilityResult);

Categories