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;
Related
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;
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);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Able to see a variable in print_r()'s output, but not sure how to access it in code
I want to 'echo' a little bit more nested object.
I saw several posts to this question - but this is freaking me out.
I got an Array/Object named 'arrResult' and the print_r(arrResult) output is:
Array
(
[status] => stdClass Object
(
[code] => 0
[message] => Success
)
[result] => Array
(
[0] => stdClass Object
(
[base] => stdClass Object
(
[id] => 3
[created] => 2012-11-11 12:11:07
[start] => 2012-11-11
)
[pos] => Array
(
[0] => stdClass Object
(
[id] => 4
[invoices_id] => 3
[article_id] => 1
[quantity] => 1
[unit] => Monate
[pos_txt] => Paketname
)
)
[summ] => stdClass Object
(
[net] => 2.52
[discount] => 0
[tax] => 0.47899159663865
[gross] => 3
[rounded_net] => 2.52
)
)
[1] => stdClass Object
(
[base] => stdClass Object
(
[id] => 2
[created] => 2012-11-11 12:10:39
[start] => 2012-11-11
)
[pos] => Array
(
[0] => stdClass Object
(
[id] => 3
[invoices_id] => 2
[article_id] => 2
[quantity] => 1
[unit] => Monate
[pos_txt] => Paketname2
)
)
[summ] => stdClass Object
(
[net] => 5.04
[discount] => 0
[tax] => 0.95798319327731
[gross] => 6
[rounded_net] => 5.04
)
)
)
)
I want to echo all [pos].
Something like 'echo arrResult[result][0][pos][0]->pos_txt', 'echo arrResult[result][0][pos][1]->pos_txt',...
I thought about this:
foreach ((array)$arrResult['result'] as $key => $contract) {
foreach ($contract as $key => $objPos){
echo $objPos->pos_txt;
}
}
My brain doesn't get it.
Can someone help me with this?
You can use
echo "<pre>";
foreach(array_map(function($v){ return $v->pos ;}, $data['result']) as $list)
{
foreach($list as $objPos)
echo $objPos->pos_txt,PHP_EOL;
}
Output
Paketname
Paketname2
See Live Demo
Use xdebug extension, it's something what you must have for development anyway, it slightly changes how var_dump works and I believe it will solve your problem
see xdebug documentation
$num = count($arrResult['result']);
for ($i=0; $i <= $num; $i++) {
echo $arrResult['result'][$i]->pos[0]->pos_txt;
}
I need to loop through the items as an array within the SimpleXMLElement Object below but cannot seem to access it using $order->order->order->items. I can access the delivery and billing addresses using the same format, ie. $order->order->order->delivery_address and expected to get to the items array in the same way. However, I get an empty SimpleXMLElement Object when I print_r($order->order->order->items)
SimpleXMLElement Object
(
[order] => SimpleXMLElement Object
(
[id] => 860268
[shopkeeper_orderno] => 1001
[customer] => 797476
[creationdate] => Apr 19 2012 10:36:38:100AM
[reference] => k2koju45rmaqfl45n20xbkmq
[net] => 1500
[vat] => 17.5
[status] => 0
[isnew] => 1
[deductions] => 0
[postage] => 1
[paymentmethod] => PayPal
[instructions] => SimpleXMLElement Object
(
)
[errors] => SimpleXMLElement Object
(
)
[kashflow_synch] => 0
[order] => Array
(
[0] => SimpleXMLElement Object
(
[billing_address] => SimpleXMLElement Object
(
[0] =>
)
)
[1] => SimpleXMLElement Object
(
[delivery_address] => SimpleXMLElement Object
(
[0] =>
)
)
[2] => SimpleXMLElement Object
(
[items] => Array
(
[0] => SimpleXMLElement Object
(
[id] => 1285158
[headerID] => 860268
[productID] => 4867690
[description] => TEST ORDERING PF NODES - Special Offer Price
[net] => 1400
[vat] => 0
[qty] => 1
[formID] => -1
)
[1] => SimpleXMLElement Object
(
[id] => 1285159
[headerID] => 860268
[productID] => 4959678
[description] => Wedding dress
[net] => 100
[vat] => 17.5
[qty] => 1
[formID] => -1
)
)
)
)
[postage_tax] => 0
[dispatched] => 0
[paybyotherid] => -1
[ip] => 81.168.43.121
[wheredidyouhearid] => -1
)
)
EDIT: I just saw you made a mistake with the naming, the parent needs to be called <orders> and the sub items <order>
The SimpleXMLElement seems to be empty, in fact it's usually filled but not displayed when dumping (whoever thought of this crazy behaviour)
Can you try this?
foreach($order->orders->order as $order) { // should be orders then
echo $item->getName();
}
Or try it with SimpleXMLElement::children()
your items are actually on the second offset of the order array.
I'd just use the xPath to process these.
foreach($xmlObject->xpath('/order/order[2]/items') as $item)
{
// Do something with my $item
}
You can use a loop like the one below, then all you need to do is $items->id
foreach($order->children()->children()->items as $items)
{
}
Using Dan Lees suggestion I tried SimpleXMLElement::children() and did the below which works
foreach ($order->children() as $order) {
foreach ($order->children() as $order_details) {
foreach ($order_details->children() as $order_items) {
echo $order_items->id;
}
}
}
For the life of me I can not figure out how to access the values of this array. Every example the stdClass Object has some type of value. If I try for example $obj->0->0->city; I get an error.
Can someone show me a example how to access [city] => toronto or even [date_created] => 2011-05-03 14:33:58?
I also tried this with no luck.
$object = $buy[1];
$title = $object->title[0];
echo "$title";
Thanks
This is what the api gives me.
stdClass Object
(
[id] => 1
[name] => toronto
[date_modified] => 2011-03-08 13:07:10
[tax_rate_provincial] =>
)
<br/>
Array
(
[0] => stdClass Object
(
[0] => stdClass Object
(
[id] => 28131844
[full_date] => 20110506
[end_date] => 20110511
[city] => toronto
[saved] => 1651
[discount_percentage] => 52
[deal_option] => Array
(
[0] => stdClass Object
(
[id] => 2600
[title] =>
[date_modified] => 0000-00-00 00:00:00
[date_created] => 2011-05-03 14:33:58
[value] => 3150
[price] => 1499
[deal_id] => 28131844
[is_default] => 0
)
)
[options] =>
[option_quantity] =>
[option_remaining] =>
[purchase_limit] => 1
[gift_limit] => 0
There is a special evil syntax to bypass numeric object attributes:
print $obj->{'0'}->{'0'}->city;
Is the correct syntax, and equivalent to the path you already determined.
Your second example is an array however, so it's probably:
print $array[0]->{'0'}->city;
The alternative is always to just foreach over a specific level - that works for objects and arrays likewise.