How to loop through a SimpleXMLElement object in Php? - php

How to loop through a SimpleXMLElement object?
This is what I have:
I tried in many ways to iterate, but no success:
SimpleXMLElement Object
(
[products] => SimpleXMLElement Object
(
[product] => Array
(
[0] => SimpleXMLElement Object
(
[product_name] => PROD1
[price] => 100.2
[has_special_price] => true
[special_price_desc] => DESC SPECIAL PRICE
[id_product_remote] => CUSTOMID-01
[decimals] => 0
[category_key] => 1
[packaging_key] => 2
[stock_type_key] => 1
)
[1] => SimpleXMLElement Object
(
[product_name] => PROD2
[price] => 200.2
[has_special_price] => false
[special_price_desc] => DEsdSC SPECIAL PRICE
[id_product_remote] => CUSTOMID-02
[decimals] => 0
[category_key] => 1
[packaging_key] => 2
[stock_type_key] => 1
)
)
)
[products_client] => SimpleXMLElement Object
(
[product_client] => Array
(
[0] => SimpleXMLElement Object
(
[id_client_remote] => CUSTOMCLIENT-01
[id_product_remote] => CUSTOMID-01
[custom_price] => 0
)
[1] => SimpleXMLElement Object
(
[id_client_remote] => CUSTOMCLIENT-02
[id_product_remote] => CUSTOMID-02
[custom_price] => 1
)
)
)
)

What have you tried?
foreach($xml_object->products->product as $key => $product){
echo $product->product_name;
}

Try doing:
foreach($yourVar->products->product as $key => $val) {
echo $val->product_name; /// and so on
}

Related

Multidimensional array value if exits

I want to bring results that have a status 1 in the array.(Sorry i don't speak english)
My array is;
[10100002] => Array
(
[0] => stdClass Object
(
[ID] => 664
[barcode] => 10100002
[status] => 0
)
[1] => stdClass Object
(
[ID] => 1339
[barcode] => 10100002
[status] => 0
)
)
[10100004] => Array
(
[0] => stdClass Object
(
[ID] => 1116
[barcode] => 10100004
[status] => 1
)
[1] => stdClass Object
(
[ID] => 1826
[barcode] => 10100004
[status] => 0
)
)
in 10100002 two status is 0 but second array found status 1. if the status value is 1 in multiple arrays i like result this ;
[10100004] => Array
(
[0] => stdClass Object
(
[ID] => 1116
[barcode] => 10100004
[status] => 1
)
[1] => stdClass Object
(
[ID] => 1826
[barcode] => 10100004
[status] => 0
)
)
My code is here;
$result = array();
foreach ($fetch_data as $value) {
if($value->status== 1)
$result[$value->barcode][] = $value;
}
// this give me only one result. Output;
[10100004] => Array
(
[0] => stdClass Object
(
[ID] => 1116
[barcode] => 10100004
[status] => 1
)
)
You can use array_walk
$arr = Array(
'10100002' => Array
(
'0' => Array
(
'ID' => 664,
'barcode' => 10100002,
'status' => 0
),
'1' => Array
(
'ID' => 1339,
'barcode' => 10100002,
'status' => 0
)
),
'10100004' => Array
(
'0' => Array
(
'ID' => 1116,
'barcode' => 10100004,
'status' => 1
),
'1' => Array
(
'ID' => 1826,
'barcode' => 10100004,
'status' => 0
)
)
);
$res = [];
array_walk($arr, function($v, $k) use (&$res){
foreach($v as $key => $value){
if($value['status']){
!empty($value['status']) ? ($res[$k][] = (object)$v[$key]) : '';
}
}
});
echo '<pre>';
print_r($res);
Output
Array
(
[10100004] => Array
(
[0] => stdClass Object
(
[ID] => 1116
[barcode] => 10100004
[status] => 1
)
)
)
LIVE DEMO

Need to access array values from array in PHP

I got array value from API.
model\Quote Object
(
[quote_reference:protected] => 12345
[proposals:protected] => model\Proposals Object
(
[proposal:protected] => Array
(
[0] => model\Proposal Object
(
[id:protected] => prp_3453453453sdfsd
[fees_included:protected] => 1
[charged_amount:protected] => model\Amount Object
(
[currency:protected] => USD
[amount:protected] => 105.00
)
[credited_amount:protected] => model\Amount Object
(
[currency:protected] => MAD
[amount:protected] => 1000.00
)
[principal_amount:protected] => model\Amount Object
(
[currency:protected] => USD
[amount:protected] => 100.00
)
[expiration_date:protected] => DateTime Object
(
[date] => 2016-04-07 02:12:55.682000
[timezone_type] => 1
[timezone] => -05:00
)
[destination_service_tag:protected] =>
[corridor_tag:protected] =>
[resource_type:protected] => proposal
[additional_data_list:protected] => model\AdditionalDataList Object
(
[data:protected] => model\Data Object
(
[data_field:protected] => Array
(
[0] => model\DataField Object
(
[name:protected] => 811
[value:protected] => 123
)
[1] => model\DataField Object
(
[name:protected] => 851
[value:protected] => 456
)
)
)
[item_count:protected] => 2
[resource_type:protected] => list
)
[wholesale_fx_rate:protected] =>
)
[1] => model\Proposal Object
(
[id:protected] => prp_456sgdfgsdfg [fees_included:protected] => 1
[charged_amount:protected] => model\Amount Object
(
[currency:protected] => USD
[amount:protected] => 100.00
)
[credited_amount:protected] => model\Amount Object
(
[currency:protected] => MAD
[amount:protected] => 950.00
)
[principal_amount:protected] => model\Amount Object
(
[currency:protected] => USD
[amount:protected] => 100.00
)
[expiration_date:protected] => DateTime Object
(
[date] => 2016-04-07 02:22:55.682000
[timezone_type] => 1
[timezone] => -05:00
)
[destination_service_tag:protected] =>
[corridor_tag:protected] =>
[resource_type:protected] => proposal
[additional_data_list:protected] => model\AdditionalDataList Object
(
[data:protected] => model\Data Object
(
[data_field:protected] => Array
(
[0] => model\DataField Object
(
[name:protected] => 811
[value:protected] => 123
)
[1] => model\DataField Object
(
[name:protected] => 851
[value:protected] => 456
)
)
)
[item_count:protected] => 2
[resource_type:protected] => list
)
[wholesale_fx_rate:protected] =>
)
)
)
)
I need to access both proposal array values at 0 and 1 index position.
I Tried:
$arrayobject->proposals->proposal[0];
Getting error:
Fatal error: Cannot access protected property model\Quote::$proposal
Any idea or help would be appreciated.
Thanks!

SimpleXMLElement Object and hidden child elements

I'm asking for explanation of the following behavior behind the scene in PHP.
I have a SimpleXMLElement Object that I print_r(); Here is the result:
Array
(
[Shipments] => SimpleXMLElement Object
(
[Shipment] => SimpleXMLElement Object
(
[Rates] => SimpleXMLElement Object
(
[Rate] => SimpleXMLElement Object
(
[Service] => C
[ServiceCharge] => 10.18
[ServiceChargeDetails] => SimpleXMLElement Object
(
[BaseCharge] => 8.33
[CODCharge] => 0
[DeclaredCharge] => 0
[AdditionalCharges] => 1.85
[SaturdayCharge] => 0
)
[FuelCharge] => 0
[TotalCharge] => 10.18
[BilledWeight] => 6
[TransitDays] => 1
[ExpectedDeliveryDate] => 20160121
[CommitTime] => 17:00:00
[RateZone] => 2
[GlobalRate] => 10.66
)
)
[UID] => ID1
[Delzip] => 95126
[PUZip] => 95035
[Declared] => 100
[Residential] => true
[COD] => 0
[SaturdayDel] => false
[Weight] => 2
[DIM] => SimpleXMLElement Object
(
[Length] => 10
[Width] => 12
[Height] => 13
)
[Error] => SimpleXMLElement Object
(
)
)
[Error] => SimpleXMLElement Object
(
)
)
)
Please take a look at the "TotalCharge". It doesn't seem to have any child elements however when I do the following:
print_r($response['Shipments']->Shipment->Rates->Rate->TotalCharge);
Here is what I get:
SimpleXMLElement Object
(
[0] => 10.18
)
I'm trying to understand how did the [0] get there and why the initial print_r didn't show it?
Thanks!

Pull data from a PHP object

I am trying to pull data from a PHP object, but I can't get all the data which is needed.
Object $hotel1 is:
stdClass Object (
[processId] => HH-24896940
[hotelCode] => TRN367
[availabilityStatus] => InstantConfirmation
[totalPrice] => 398
[totalTax] => 0
[totalSalePrice] => 0
[currency] => EUR
[boardType] => Half Board
[rooms] => Array ( [0] => stdClass Object (
[roomCategory] => Double Standard [paxes] => Array ( [0] => stdClass Object ( [paxType] => Adult [age] => 30 )
[1] => stdClass Object (
[paxType] => Adult [age] => 30 ) )
[totalRoomRate] => 398
[ratesPerNight] => Array ( [0] => stdClass Object (
[date] => 2015-01-27
[amount] => 398 ) ) ) ) )
Code used is :
<?php
foreach ($availHotels as $hotel1) {
if ($hotel1->hotelCode === $code1) {
break;
}
}
?>
And I pull data with:
$hotel1->boardType , $hotel1->totalPrice
But when I am trying to get roomCategory is not working with
$hotel1->roomCategory
"roomCategory" is nested in "room".
Here is an example how to get what you expect:
$obj = (object) array(
'boardType' => 'foo',
'room' => array(
(object) array(
'roomCategory' => 'bar'
)
)
);
print_r($obj);
echo $obj->boardType;
echo $obj->room[0]->roomCategory;
$obj would be $hotel1 in your case.

How to access #attributes data in SimpleXMLElement?

I am trying to get some specific data from a xml file.
php Code
$url = 'http://amdata.adlibsoft.com/wwwopac.ashx?
database=AMcollect&search=priref=397*&xmltype=grouped';
$xml = file_get_contents($url);
$xml = new SimpleXMLElement($xml);
doing
print_r ($xml);
will return
SimpleXMLElement Object
(
[recordList] => SimpleXMLElement Object
(
[record] => SimpleXMLElement Object
(
[#attributes] => Array
(
[priref] => 397
[created] => 2013-11-25T14:03:22
[modification] => 2013-11-25T18:01:23
[selected] => False
)
[acquisition.date] => 1860-12-24
[acquisition.method] => legaat
[collection] => Fodor, collectie Carel Joseph
[credit_line] => Amsterdam Museum, legaat C.J. Fodor
[dimension] => Array
(
[0] => SimpleXMLElement Object
(
[dimension.part] => SimpleXMLElement Object
(
)
[dimension.type] => hoogte a
[dimension.unit] => cm
[dimension.value] => 65.5
)
[1] => SimpleXMLElement Object
(
[dimension.part] => SimpleXMLElement Object
(
)
[dimension.type] => breedte a
[dimension.unit] => cm
[dimension.value] => 97.5
)
[2] => SimpleXMLElement Object
(
[dimension.part] => SimpleXMLElement Object
(
)
[dimension.type] => hoogte b
[dimension.unit] => cm
[dimension.value] => 51.3
)
[3] => SimpleXMLElement Object
(
[dimension.part] => SimpleXMLElement Object
(
)
[dimension.type] => breedte b
[dimension.unit] => cm
[dimension.value] => 84.1
)
[4] => SimpleXMLElement Object
(
[dimension.part] => SimpleXMLElement Object
(
)
[dimension.type] => hoogte c
[dimension.unit] => cm
[dimension.value] => 40.4
)
[5] => SimpleXMLElement Object
(
[dimension.part] => SimpleXMLElement Object
(
)
[dimension.type] => breedte c
[dimension.unit] => cm
[dimension.value] => 80.7
)
)
[maker] => Array
(
[0] => SimpleXMLElement Object
(
[creator] => Kaiser, Johann Wilhelm (I)
[creator.date_of_birth] => 1813-01-05
[creator.date_of_death] => 1900-11-29
[creator.qualifier] => SimpleXMLElement Object
(
)
[creator.role] => graveur
)
[1] => SimpleXMLElement Object
(
[creator] => Helst, Bartholomeus van der
[creator.date_of_birth] => 1613
[creator.date_of_death] => 1670
[creator.qualifier] => naar
)
[2] => SimpleXMLElement Object
(
[creator] => Kunsthandel Frans Buffa & Zonen
[creator.date_of_birth] => SimpleXMLElement Object
(
)
[creator.date_of_death] => SimpleXMLElement Object
(
)
[creator.qualifier] => SimpleXMLElement Object
(
)
[creator.role] => uitgever
)
)
[material] => papier
[object_category] => prentencollectie
[object_name] => Array
(
[0] => gravure
[1] => ets
[2] => prent
)
[object_number] => A 11259
[part_of_reference] => KA 22389 & A 11217 t/m A 11265
[priref] => 397
[production.date.end] => 1860
[production.date.start] => 1849
[technique] => gegraveerd
[title] => De schuttersmaaltijd
)
)
[diagnostic] => SimpleXMLElement Object
(
[hits] => 1
[xmltype] => Grouped
[link_resolve_time] => 15.5801
[first_item] => 1
[search] => priref Equals 397*
[sort] => SimpleXMLElement Object
(
)
[limit] => 1
[hits_on_display] => 1
[response_time] => 0
[xml_creation_time] => 15.5801
[dbname] => collect
[dsname] => intern
[cgistring] => SimpleXMLElement Object
(
[param] => AMcollect
)
)
)
Now let's say I want to get the priref I tried the following things
echo($xml->record->priref);
echo $xml->record['priref'];
Both gave no result(no error and nothing being displayed)
then I tried
echo $xml->record->attributes()->priref;
and got "Node no longer exists"
This will get you attributes array:
echo $xml->recordList->record[0]->attributes();
Make use of a foreach and access it like the key-value pair to display the needed value.
<?php
$url = 'http://amdata.adlibsoft.com/wwwopac.ashx?
database=AMcollect&search=priref=397*&xmltype=grouped';
$xml = file_get_contents($url);
$xml = simplexml_load_string($xml);
echo "<pre>";
foreach ($xml->recordList->record->attributes() as $k=>$a)
{
if($k=='priref') { echo $a; break;}
}
OUTPUT :
397

Categories