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;
}
}
}
Related
I have a xml and could get the arrays with the data I need with xpath after loaded the xml with simplexml_load_file.
I tried it this way : Access #attributes data in SimpleXMLElement in PHP
with my XML to array I still cant access the nodes, could someone please check my Code: thanks
$result2 = $xml->xpath("//file[#Catid='151']");
that is giving this array:
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[path] => export/freexml.int/DE/4757.xml
[Product_ID] => 4757
[Updated] => 20170902053143
[Quality] => ICECAT
[Supplier_id] => 3
[Prod_ID] => TT34MUK
[Catid] => 151
[On_Market] => 1
[Model_Name] => THINKPAD T23 P3-1.13G
[Product_View] => 12655
[HighPic] => http://images.icecat.biz/img/norm/high/4757-6880.jpg
[HighPicSize] => 4138
[HighPicWidth] => 200
[HighPicHeight] => 150
[Date_Added] => 20050715000000
[Limited] => No
)
[EAN_UPCS] => SimpleXMLElement Object
(
[EAN_UPC] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Value] => 3606503209062
[IsApproved] => 0
)
)
)
[Country_Markets] => SimpleXMLElement Object
(
[Country_Market] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Value] => LU
)
)
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[path] => export/freexml.int/DE/41895.xml
[Product_ID] => 41895
[Updated] => 20170902052843
[Quality] => ICECAT
[Supplier_id] => 7
[Prod_ID] => LX.T2606.067
[Catid] => 151
[On_Market] => 1
[Model_Name] => TRAVELMATE 432LC P4-2.53G
[Product_View] => 12056
[HighPic] => http://images.icecat.biz/img/norm/high/41895-65.jpg
[HighPicSize] => 14404
[HighPicWidth] => 330
[HighPicHeight] => 290
[Date_Added] => 20050715000000
[Limited] => No
)
[Country_Markets] => SimpleXMLElement Object
(
[Country_Market] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Value] => DE
)
)
)
)
How can I access the values like 'path' and so on? I have problems with the
[0] => SimpleXMLElement Object so what is the Name of the node?
echo (string)$result2->0[0]->attributes()->path;
didnt work.....
thanks
Although it would be easier to check with the XML, you will probably find the correct notation is...
echo (string)$result2[0]->attributes()->path;
This assumes that you want the first item that XPath has found.
You may also find
echo (string)$result2[0]['path'];
Works (but not always).
I'm trying to get data out of some JSON DATA. I'm using the following lines to decode it right now
$json_array = (array)(json_decode($response));
When I print my JSON Decoded array, I have the following data below:
I would like to get the details from the details section, where there is multiple sets of from/to_date's, and up/down numbers. Nothing I seem to do works though to get me to that data. I can print out the data from other areas like usage, but, I can't get into the details.
Array (
[resp_code] => SUCCESS
[caller_ref] => 2017092002282130006180
[server_ref] => 2017092002282169760291
[data] => stdClass Object (
[type] => monthly
[group_id] => 19
[device_id] => 32
[sn] => sn1234
[usages] => Array (
[0] => stdClass Object (
[from_date] => 2017-09-01T00:00:00
[to_date] => 2017-09-30T23:59:59
[up] => 22370
[down] => 119217
[ts] => 2017-09-01T00:00:00
)
)
[details] => stdClass Object (
[3] => Array (
[0] => stdClass Object (
[from_date] => 2017-09-01T00:00:00
[to_date] => 2017-09-30T23:59:59
[up] => 5522
[down] => 40301
[ts] => 2017-09-01T00:00:00
)
)
[2] => Array (
[0] => stdClass Object (
[from_date] => 2017-09-01T00:00:00
[to_date] => 2017-09-30T23:59:59
[up] => 6905
[down] => 32029
[ts] => 2017-09-01T00:00:00
)
)
)
)
)
Whats wrong with objects?
$obj = json_decode($response);
echo $obj->data->details[0]->from_date;
Or to loop it:
foreach ($obj->data->details as $item) {
echo $item->from_date;
// same goes for: to_date, up etc.
}
Simple and sexy!
Update:
It looks as if $item would be an array as well, so if you have problems try:
foreach ($obj->data->details as $item) {
echo $item[0]->from_date;
// same goes for: to_date, up etc.
}
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);
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;
}
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.