Retrieve a value from a SimpleXML object - php

Excuse me for asking such a "noob" question, but I have been trying to find a simple example of how to loop through my returned xml for the past few hours and I'm getting no where. I just want to be able to cycle through the xml and pull the 'Amount' attribute and 'AmazonOrderId' attribute for each Order. I don't know how to loop, nor how to grab the pertinent data.
SimpleXMLElement Object
(
[ListOrdersResult] => SimpleXMLElement Object
(
[Orders] => SimpleXMLElement Object
(
[Order] => Array
(
[0] => SimpleXMLElement Object
(
[ShipmentServiceLevelCategory] => SecondDay
[OrderTotal] => SimpleXMLElement Object
(
[Amount] => 5.93
[CurrencyCode] => USD
)
[SellerOrderId] => 107-1261608-7067458
[FulfillmentChannel] => AFN
[BuyerEmail] => 5qhs64ktb88pdsj#marketplace.amazon.com
[OrderStatus] => Shipped
[BuyerName] => Derrick D Vann
[ShipServiceLevel] => SecondDay
[LastUpdateDate] => 2013-03-13T02:20:31Z
[PurchaseDate] => 2013-03-11T06:14:40Z
[NumberOfItemsUnshipped] => 0
[MarketplaceId] => ATVPDKIKX0DER
[SalesChannel] => Amazon.com
[ShippingAddress] => SimpleXMLElement Object
(
[Phone] => 202 746-2567
[PostalCode] => 20001-4040
[Name] => Derrick Vann
[CountryCode] => US
[StateOrRegion] => DC
[AddressLine1] => 2120 Vermont Ave NW Apt 117
[City] => Washington
)
[NumberOfItemsShipped] => 1
[AmazonOrderId] => 107-1261608-7067458
[PaymentMethod] => Other
)
[1] => SimpleXMLElement Object
(
[ShipmentServiceLevelCategory] => Expedited
[OrderTotal] => SimpleXMLElement Object
(
[Amount] => 23.30
[CurrencyCode] => USD
)
[SellerOrderId] => 104-9066827-4446667
[FulfillmentChannel] => AFN
[BuyerEmail] => 6kfc88nrsnm83fq#marketplace.amazon.com
[OrderStatus] => Shipped
[BuyerName] => Quoc Bui
[ShipServiceLevel] => Expedited
[LastUpdateDate] => 2013-03-13T09:34:26Z
[PurchaseDate] => 2013-03-11T08:07:13Z
[NumberOfItemsUnshipped] => 0
[MarketplaceId] => ATVPDKIKX0DER
[SalesChannel] => Amazon.com
[ShippingAddress] => SimpleXMLElement Object
(
[Phone] => (02) 9560 3639
[PostalCode] => 2204
[Name] => Quoc Minh Bui
[CountryCode] => AU
[StateOrRegion] => New South Wales
[AddressLine1] => 19 Centennial St
[City] => Marrickville
)
[NumberOfItemsShipped] => 1
[AmazonOrderId] => 104-9066827-4446667
[PaymentMethod] => Other
)
...

SimpleXML is just great, isn't it?
foreach ($xml->ListOrdersResult->Orders->Order as $order) {
$amazonOrderId = (string) $order->AmazonOrderId;
$orderTotal = (string) $order->OrderTotal->Amount;
}
I use (string) type casting because you'd get SimpleXMLElements back for those scalar values otherwise.

pull the 'Amount' attribute and 'AmazonOrderId' attribute for each Order.
If I take you word-by-word, then this is what you're looking for:
$allThoseAttributes = $xml->xpath('//Order/#Amount|//Order/#AmazonOrderId');
However, more likely you're looking for:
$orders = [];
foreach ($xml->xpath('//Order') as $order) {
$orders[] = [
'amazon' => (string) $order->AmazonOrderId
'total' => (string) $order->OrderTotal->Amount;
];
}
because what you call attributes are infact elements. If you want to learn more about XML and Xpath, this is a good read: XPath

Related

How to display data in this format from multiple associative array php

I'm trying to find out in a simple way to display a clean like this format
Having a hard time to spread the data in this associative array got this from an API
Array (
[wind] => Array (
[speed] => 5
[direction] => North West
[directionDegrees] => 310
[unit] => KT
)
[visibility] => Array (
[mainVisibility] => 10SM
)
[clouds] => Array (
[0] => Array (
[height] => 25000
[quantity] => few
)
)
[cavok] =>
[remark] => automated station with a precipitation discriminator sea level pressure of 1021.7 HPa hourly temperature of 17.2°C and dew point of -1.1°C
[day] => 6
[time] => 22:52:00
[airport] => Array (
[name] => Hartsfield Jackson Atlanta International Airport
[city] => Atlanta
[country] => Array (
[name] => United States
)
[iata] => ATL
[icao] => KATL
[latitude] => 33.6367
[longitude] => -84.428101
[altitude] => 1026
[timezone] => -5
[dst] => A
)
[message] => KATL 062252Z 31005KT 10SM FEW250 17/M01 A3017 RMK AO2 SLP217 T01721011
[station] => KATL
[temperature] => 17
[dewPoint] => -1
[altimeter] => 1021
[nosig] =>
[auto] =>
[amendment] =>
[nil] =>
[corrected] =>
[cancelled] =>
)
I'm not good at programming as this is our assignment from our school I'm looking at to loop the array but I can't seem to find a way how to do it.
You can use this parser source code and make your own solution on the base
https://github.com/SafranCassiopee/php-metar-decoder
or simple way to get field
$arr = '$yourDataArrayHere$';
$metarRw = $arr['message']
echo 'Metar message '.$metarRw;
$airportName = $arr['airport']['name']
echo 'Airport name '.$airportName;
etc.

PHP Object with array of objects finding key/value and linking it with another key/value pair

PROGRESS: Almost there... Need help on the last bit!!!
$users = $client->getAccounts();
$num = count($users);
for ( $x=0; $x < $num; $x++){
foreach($users[$x] as $y => $y_value) {
if($y_value == iLy){
print_r($y);
echo '<br>';
print_r($x);
echo '<br>';
print_r($user[$x]["id"]);
}
}
}
Above is the latest bit of code I have written and I have been to locate the array value or the array I am looking for by search by name. print_r($user[$x]["id"]);
Results in:
Warning: Illegal string offset 'id' in /home/examplesite/BLANK.com/CBapi.php on line 37
I used this simplification to discover the logic for my solution:
This works
<?php
$age = array (array("name"=>"iLy", "id"=>"37", "balance"=>"43"),
array("name"=>"jim", "id"=>"67", "balance"=>"47"));
echo $age[0]['id'];
$num = count($age);
for ( $x=0; $x < $num; $x++){
foreach($age[$x] as $y => $y_value) {
if($y_value == 43){
print_r($y);
echo '<br>';
print_r($x);
echo '<br>';
print_r($age[$x]["id"]);
}
}
}
?>
I believe I am just calling the wrong Key Name.
++++++++++++++++++++++ Original Post Below ++++++++++++++++++++++
Current code:
$configuration = Configuration::apiKey($apiKey, $apiSecret);
$configuration->setApiUrl(Configuration::SANDBOX_API_URL);
$client = Client::create($configuration);
$users = $client->getAccounts();
I have been struggling with this for four days now... I used function below and it returns an object which contains an array of objects. My goal is to search through the collection of objects for a key and value, (For example to search the name of a current user:$currentuser = 'iLy'; $user = $currentuser; Then I could identify the array value, which would be $users[2] for our example 'iLy' to search the key value 'id', to get the $accountId.
Ideally I could search 'name' => iLy and have it return the 'id' => 'xxxxxxx' in that same array.
When I create accounts this is the only way I have been able to figure out how to get the accountID, and I haven't found any other API calls to get the account by name. I have only been able to isolate one object using $users[2]. I know this may be a repeat post, but I haven't been able to make any of the other solutions work.
PHP - find entry by object property from a array of objects , Array of PHP Objects
Maybe I am using the wrong solutions or applying them wrong. Any help would be greatly appreciated!
public function getAccounts(array $params = [])
{
return $this->getAndMapCollection('/v2/accounts',$params,'toAccounts');
}
Returns this:
Coinbase\\Wallet\\Resource\\ResourceCollection Object (
[previousUri:Coinbase\\Wallet\\Resource\\ResourceCollection:private] =>
[nextUri:Coinbase\\Wallet\\Resource\\ResourceCollection:private] =>
[resources:Coinbase\\Wallet\\Resource\\ResourceCollection:private] => Array (
[0] => Coinbase\\Wallet\\Resource\\Account Object (
[name:Coinbase\\Wallet\\Resource\\Account:private] => Jennaod3
[primary:Coinbase\\Wallet\\Resource\\Account:private] =>
[type:Coinbase\\Wallet\\Resource\\Account:private] => wallet
[currency:Coinbase\\Wallet\\Resource\\Account:private] => BTC
[balance:Coinbase\\Wallet\\Resource\\Account:private] => Coinbase\\Wallet\\Value\\Money Object (
[amount:Coinbase\\Wallet\\Value\\Money:private] => 0.00000000
[currency:Coinbase\\Wallet\\Value\\Money:private] => BTC
)
[nativeBalance:Coinbase\\Wallet\\Resource\\Account:private] => Coinbase\\Wallet\\Value\\Money Object (
[amount:Coinbase\\Wallet\\Value\\Money:private] => 0.00
[currency:Coinbase\\Wallet\\Value\\Money:private] => USD
)
[createdAt:Coinbase\\Wallet\\Resource\\Account:private] => DateTime Object (
[date] => 2016-05-24 04:55:41.000000
[timezone_type] => 2
[timezone] => Z
)
[updatedAt:Coinbase\\Wallet\\Resource\\Account:private] => DateTime Object (
[date] => 2016-05-24 04:55:41.000000
[timezone_type] => 2
[timezone] => Z
)
[id:Coinbase\\Wallet\\Resource\\Resource:private] => 0d41fc45-0a53-58cb-9931-c9a33f520963
[resource:Coinbase\\Wallet\\Resource\\Resource:private] => account
[resourcePath:Coinbase\\Wallet\\Resource\\Resource:private] => /v2/accounts/0d41fc45-0a53-58cb-9931-c9a33f520963
[rawData:Coinbase\\Wallet\\Resource\\Resource:private] => Array (
[id] => 0d41fc45-0a53-58cb-9931-c9a33f520963
[name] => Jennaod3
[primary] =>
[type] => wallet
[currency] => BTC
[balance] => Array (
[amount] => 0.00000000
[currency] => BTC
)
[native_balance] => Array (
[amount] => 0.00
[currency] => USD
)
[created_at] => 2016-05-24T04:55:41Z
[updated_at] => 2016-05-24T04:55:41Z
[resource] => account
[resource_path] => /v2/accounts/0d41fc45-0a53-58cb-9931-c9a33f520963
)
)
[1] => Coinbase\\Wallet\\Resource\\Account Object (
[name:Coinbase\\Wallet\\Resource\\Account:private] => jenna works to
[primary:Coinbase\\Wallet\\Resource\\Account:private] =>
[type:Coinbase\\Wallet\\Resource\\Account:private] => wallet
[currency:Coinbase\\Wallet\\Resource\\Account:private] => BTC
[balance:Coinbase\\Wallet\\Resource\\Account:private] => Coinbase\\Wallet\\Value\\Money Object (
[amount:Coinbase\\Wallet\\Value\\Money:private] => 0.00000000
[currency:Coinbase\\Wallet\\Value\\Money:private] => BTC
)
[nativeBalance:Coinbase\\Wallet\\Resource\\Account:private] => Coinbase\\Wallet\\Value\\Money Object (
[amount:Coinbase\\Wallet\\Value\\Money:private] => 0.00
[currency:Coinbase\\Wallet\\Value\\Money:private] => USD
)
[createdAt:Coinbase\\Wallet\\Resource\\Account:private] => DateTime Object (
[date] => 2016-05-22 13:37:16.000000
[timezone_type] => 2
[timezone] => Z
)
[updatedAt:Coinbase\\Wallet\\Resource\\Account:private] => DateTime Object (
[date] => 2016-05-22 13:37:16.000000
[timezone_type] => 2
[timezone] => Z
)
[id:Coinbase\\Wallet\\Resource\\Resource:private] => e7ab48b4-bc76-513a-a78b-6d627f32f848
[resource:Coinbase\\Wallet\\Resource\\Resource:private] => account
[resourcePath:Coinbase\\Wallet\\Resource\\Resource:private] => /v2/accounts/e7ab48b4-bc76-513a-a78b-6d627f32f848
[rawData:Coinbase\\Wallet\\Resource\\Resource:private] => Array (
[id] => e7ab48b4-bc76-513a-a78b-6d627f32f848
[name] => jenna works to
[primary] =>
[type] => wallet
[currency] => BTC
[balance] => Array (
[amount] => 0.00000000
[currency] => BTC
)
[native_balance] => Array (
[amount] => 0.00
[currency] => USD
)
[created_at] => 2016-05-22T13:37:16Z
[updated_at] => 2016-05-22T13:37:16Z
[resource] => account
[resource_path] => /v2/accounts/e7ab48b4-bc76-513a-a78b-6d627f32f848
)
)
[2] => Coinbase\\Wallet\\Resource\\Account Object (
[name:Coinbase\\Wallet\\Resource\\Account:private] => iLy
[primary:Coinbase\\Wallet\\Resource\\Account:private] =>
[type:Coinbase\\Wallet\\Resource\\Account:private] => wallet
[currency:Coinbase\\Wallet\\Resource\\Account:private] => BTC
[balance:Coinbase\\Wallet\\Resource\\Account:private] => Coinbase\\Wallet\\Value\\Money Object (
[amount:Coinbase\\Wallet\\Value\\Money:private] => 0.00000000
[currency:Coinbase\\Wallet\\Value\\Money:private] => BTC
)
[nativeBalance:Coinbase\\Wallet\\Resource\\Account:private] => Coinbase\\Wallet\\Value\\Money Object (
[amount:Coinbase\\Wallet\\Value\\Money:private] => 0.00
[currency:Coinbase\\Wallet\\Value\\Money:private] => USD
)
[createdAt:Coinbase\\Wallet\\Resource\\Account:private] => DateTime Object (
[date] => 2016-05-22 13:33:36.000000
[timezone_type] => 2
[timezone] => Z
)
[updatedAt:Coinbase\\Wallet\\Resource\\Account:private] => DateTime Object (
[date] => 2016-05-22 13:33:36.000000
[timezone_type] => 2
[timezone] => Z
)
[id:Coinbase\\Wallet\\Resource\\Resource:private] => c95fd701-cf2b-56f7-b438-9a2f0e61b21c
[resource:Coinbase\\Wallet\\Resource\\Resource:private] => account
[resourcePath:Coinbase\\Wallet\\Resource\\Resource:private] => /v2/accounts/c95fd701-cf2b-56f7-b438-9a2f0e61b21c
[rawData:Coinbase\\Wallet\\Resource\\Resource:private] => Array (
[id] => c95fd701-cf2b-56f7-b438-9a2f0e61b21c
[name] => iLy
[primary] =>
[type] => wallet
[currency] => BTC
[balance] => Array (
[amount] => 0.00000000
[currency] => BTC
)
[native_balance] => Array (
[amount] => 0.00
[currency] => USD
)
[created_at] => 2016-05-22T13:33:36Z
[updated_at] => 2016-05-22T13:33:36Z
[resource] => account
[resource_path] => /v2/accounts/c95fd701-cf2b-56f7-b438-9a2f0e61b21c
)
)
[3] => Coinbase\\Wallet\\Resource\\Account Object (
[name:Coinbase\\Wallet\\Resource\\Account:private] => BTC Wallet
[primary:Coinbase\\Wallet\\Resource\\Account:private] => 1
[type:Coinbase\\Wallet\\Resource\\Account:private] => wallet
[currency:Coinbase\\Wallet\\Resource\\Account:private] => BTC
[balance:Coinbase\\Wallet\\Resource\\Account:private] => Coinbase\\Wallet\\Value\\Money Object (
[amount:Coinbase\\Wallet\\Value\\Money:private] => 0.10000000
[currency:Coinbase\\Wallet\\Value\\Money:private] => BTC
)
[nativeBalance:Coinbase\\Wallet\\Resource\\Account:private] => Coinbase\\Wallet\\Value\\Money Object (
[amount:Coinbase\\Wallet\\Value\\Money:private] => 1000.00
[currency:Coinbase\\Wallet\\Value\\Money:private] => USD
)
[createdAt:Coinbase\\Wallet\\Resource\\Account:private] => DateTime Object (
[date] => 2016-05-21 02:41:13.000000
[timezone_type] => 2
[timezone] => Z
)
[updatedAt:Coinbase\\Wallet\\Resource\\Account:private] => DateTime Object (
[date] => 2016-05-21 02:41:14.000000
[timezone_type] => 2
[timezone] => Z
)
[id:Coinbase\\Wallet\\Resource\\Resource:private] => 0e0dac44-6900-59e9-8183-99b9459d1205
[resource:Coinbase\\Wallet\\Resource\\Resource:private] => account
[resourcePath:Coinbase\\Wallet\\Resource\\Resource:private] => /v2/accounts/0e0dac44-6900-59e9-8183-99b9459d1205
[rawData:Coinbase\\Wallet\\Resource\\Resource:private] => Array (
[id] => 0e0dac44-6900-59e9-8183-99b9459d1205
[name] => BTC Wallet
[primary] => 1
[type] => wallet
[currency] => BTC
[balance] => Array (
[amount] => 0.10000000
[currency] => BTC
)
[native_balance] => Array (
[amount] => 1000.00
[currency] => USD
)
[created_at] => 2016-05-21T02:41:13Z
[updated_at] => 2016-05-21T02:41:14Z
[resource] => account
[resource_path] => /v2/accounts/0e0dac44-6900-59e9-8183-99b9459d1205
)
)
)
)
This was my answer and it isn't pretty. It turns out I had to make some of the variables public in the API resource file, which I'm not sure if that was a good idea, but it achieved my goal using this code:
$users = $client->getAccounts();
$num = count($users);
for ( $x=0; $x < $num; $x++){
foreach($users[$x] as $y => $y_value) {
if($y_value == iLy) {
$bae = $users[$x];
$account_id = '';
foreach ($bae as $k => $k_value) {
$account_id = $k_value;
}
}
}
}
It seems as though all my arrays were objects and the only way I could obtain the values I needed was by using foreach loops.

how to convert xml array to php array [duplicate]

This question already has answers here:
How to convert XML into array in PHP?
(12 answers)
Closed 7 years ago.
I am working on property site. My requirement is to import property info with its images and amenities and store it in to database. I've an xml array and i want to convert it into php array. I've xml array like this:-
SimpleXMLElement Object
(
[Listing] => Array
(
[0] => SimpleXMLElement Object
(
[count] => 1
[Ad_Type] => Rent
[Unit_Type] => Office
[Unit_Model] => SimpleXMLElement Object
(
)
[Primary_View] => SimpleXMLElement Object
(
)
[Unit_Builtup_Area] => 7593.00
[No_of_Bathroom] => 2
[Property_Title] => Business Center / BANKS offices/ Call Centre Offices
[Web_Remarks] => SimpleXMLElement Object
(
)
[Emirate] => Dubai
[Community] => Sheikh Zayed Road
[Property_Name] => Millennium Plaza
[Property_Ref_No] => AMB-R-1142
[Listing_Agent] => Janette Ceniza
[Listing_Agent_Phone] => 0564843282
[Listing_Date] => 2015-03-26 4:53:51 pm
[Last_Updated] => 2015-07-29 1:44:23 pm
[Bedrooms] => SimpleXMLElement Object
(
)
[Listing_Agent_Email] => consult1#khalidalattar.com
[Price] => 1200000
[Frequency] => per year
[Unit_Reference_No] => AMB-R-1142
[No_of_Rooms] => SimpleXMLElement Object
(
)
[Latitude] => 25.062252
[Longitude] => 55.130672
[unit_measure] => Sq.Ft.
[Featured] => 0
[Images] => SimpleXMLElement Object
(
[image] => Array
(
[0] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_19-1605-6d8e8f3aa6e53b45154eac91e2109ba4.jpg
[1] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_35-1605-660a612b520978e1249c29b7fb591b45.jpg
[2] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_36-1605-01d394a6a096b9a0252b30a10fd59c13.jpg
[3] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_38-1605-80df60708c007c5a85721b89c97836d8.jpg
[4] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_38-1605-1f9ffdde85af52dfc6f3548264c34ce0.jpg
[5] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_44-1605-322badce946a4883abe602bf8c221a93.jpg
[6] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_44-1605-117288432442fd54dae0e68238f39537.jpg
[7] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_54-1605-914e38a1abe6418ea10885b94e4f26de.jpg
[8] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_55-1605-fc756a57f7280678b41b07340e3d1e60.jpg
[9] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_55-1605-53eb6e2277f1b19ee40a36660086a20e.jpg
[10] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_58_36-1605-9e54faba2c9f96e31646c0660f3f0153.jpg
)
)
[Facilities] => SimpleXMLElement Object
(
[facility] => Array
(
[0] => Built in wardrobes
[1] => Covered parking
[2] => Public parking
)
)
[company_name] => Amber Real Estate
[Web_Tour] => SimpleXMLElement Object
(
)
[Threesixty_Tour] => SimpleXMLElement Object
(
)
[Audio_Tour] => SimpleXMLElement Object
(
)
[Virtual_Tour] => SimpleXMLElement Object
(
)
[QR_Code] => SimpleXMLElement Object
(
)
[company_logo] => http://crm.propspace.com/application/views/pictures/logos/16051408971828.png
[Parking] => 2
[Strno] => SimpleXMLElement Object
(
)
[PreviewLink] => http://crm.propspace.com/preview/index/14273740230306248/1605/?l_id=1576257622628120
)
)
)
There are multiple entries like this. I'd given first entry for the referrence. For converting this xml to php array I've written the below code:-
function convert_xml($xml){
$arr = array();
foreach($xml->children() as $property){
if(count($property->children()) == 0){
$arr[$property->getName()] = strval($property);
}else{
$arr[$property->getName()][] = xml2array($property);
}
}
return $arr;
}
$xml_Arr = convert_xml($xml);
echo "<pre>"; print_r($xml_Arr); exit;
I m getting the result as php array given below.
Array
(
[Listing] => Array
(
[0] => Array
(
[count] => 1
[Ad_Type] => Rent
[Unit_Type] => Office
[Unit_Model] =>
[Primary_View] =>
[Unit_Builtup_Area] => 7593.00
[No_of_Bathroom] => 2
[Property_Title] => Business Center / BANKS offices/ Call Centre Offices
[Emirate] => Dubai
[Community] => Sheikh Zayed Road
[Property_Name] => Millennium Plaza
[Property_Ref_No] => AMB-R-1142
[Listing_Agent] => Janette Ceniza
[Listing_Agent_Phone] => 0564843282
[Listing_Date] => 2015-03-26 4:53:51 pm
[Last_Updated] => 2015-07-29 1:44:23 pm
[Bedrooms] =>
[Listing_Agent_Email] => consult1#khalidalattar.com
[Price] => 1200000
[Frequency] => per year
[Unit_Reference_No] => AMB-R-1142
[No_of_Rooms] =>
[Latitude] => 25.062252
[Longitude] => 55.130672
[unit_measure] => Sq.Ft.
[Featured] => 0
[Images] => Array
(
[0] => Array
(
[image] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_58_36-1605-9e54faba2c9f96e31646c0660f3f0153.jpg
)
)
[Facilities] => Array
(
[0] => Array
(
[facility] => Public parking
)
)
[company_name] => Amber Real Estate
[Web_Tour] =>
[Threesixty_Tour] =>
[Audio_Tour] =>
[Virtual_Tour] =>
[QR_Code] =>
[company_logo] => http://crm.propspace.com/application/views/pictures/logos/16051408971828.png
[Parking] => 2
[Strno] =>
[PreviewLink] => http://crm.propspace.com/preview/index/14273740230306248/1605/?l_id=1576257622628120
)
)
)
There are 10 element inside image array, but it is returning only the last image. I want image array as well in my result.
foreach($xml->children() as $key=>$property){
if(count($property->children()) == 0){
$arr[$property->getName()] = strval($property);
}else{
$arr[$property->getName()][$key] = xml2array($property);
}
}

How to iterate through XML values that are a SimpleXMLElement Object

I have an XML stream parsed to a SimpleXMLElement Object and I am trying to iterate though the available records to use as values in a PHP page.
The parent node of [listing] currently exists twice as there are two records in the test XML (listing[0] and listing[1])
But I can not get this to work like shown on the "Basic SimpleXML usage" from the PHP Manual
<?php
$xml = simplexml_load_file('http://feed.postlets.com/Burndog/6458ec1af54f632');
This works to provide the first listing title element value:
$value1 = $xml->listing[0]->title;
echo ' here:' . $value1;
This fails to iterate through the available values:
foreach ($xml->listing->title as $title) {
echo $title;
}
?>
values from a print_r:
SimpleXMLElement Object
(
[listing] => Array
(
[0] => SimpleXMLElement Object
(
[url] => http://www.postlets.com/repb/6509636
[title] => 3BR/2BA Manufactured - Beaumont
[subtitle] => SimpleXMLElement Object
(
)
[description] => SimpleXMLElement Object
(
)
[location] => SimpleXMLElement Object
(
[street] => 1415 E 6th St
[city] => Beaumont
[zipcode] => 92223
[state] => CA
[latitude] => 33.928326
[longitude] => -116.959923
[walkscore] => 46
)
[details] => SimpleXMLElement Object
(
[money] => SimpleXMLElement Object
(
[price] => 44900
)
[property_for] => Sale
[property_use] => Residential
[property_type] => Manufactured
[year_built] => 2011
[bedrooms] => 3
[full_bathrooms] => 2
[partial_bathrooms] => 0
[sqft] => 1041
[lot_size] => 1045 sqft
[parking] => SimpleXMLElement Object
(
)
)
[photos] => SimpleXMLElement Object
(
[photo_1] => http://www.postlets.com/create/photos/20111101/082821_6509636_158803034.jpg
[photo_caption_1] => Photo 1
[photo_2] => http://www.postlets.com/create/photos/20111101/082822_6509636_3416721218.jpg
[photo_caption_2] => Photo 2
[photo_3] => http://www.postlets.com/create/photos/20111101/082822_6509636_1298858591.jpg
[photo_caption_3] => Photo 3
)
[contact] => SimpleXMLElement Object
(
)
)
[1] => SimpleXMLElement Object
(
[url] => http://www.postlets.com/repb/7066849
[title] => 2BR/1+1BA Manufactured - Beaumont
[subtitle] => SimpleXMLElement Object
(
)
[description] => SimpleXMLElement Object
(
)
[location] => SimpleXMLElement Object
(
[street] => 1415 E 6th St # 12
[city] => Beaumont
[zipcode] => 92223
[state] => CA
[latitude] => 33.929199
[longitude] => -116.959831
[walkscore] => 46
)
[details] => SimpleXMLElement Object
(
[money] => SimpleXMLElement Object
(
[price] => 56000
[hoa] => 400
)
[property_for] => Sale
[property_use] => Residential
[property_type] => Manufactured
[year_built] => 1997
[bedrooms] => 2
[full_bathrooms] => 1
[partial_bathrooms] => 1
[sqft] => 1250
[lot_size] => 3000 sqft
[property_features] => Central A/C, Dining room, Breakfast nook, Dryer
[community_features] => Covered parking
[parking] => SimpleXMLElement Object
(
)
) etc etc
Then what will it take to loop through the elements for pictures as there is more than one?
Thanks!
As you can see in your print_r output, the 'listing' field of the XML-Object is the array, not the title. So what you have to do is iterate through the listings and print out each listings title:
foreach ($xml->listing as $listing)
{
echo $listing->title;
}
To print out the pictures you'd do something like this:
foreach ($xml->listing as $listing)
{
echo "Title: " . $listing->title . "<br>";
foreach ($listing->photos->children() as $child)
{
echo $child . "<br>";
}
}

how to display individual values?

I get this from print_r($data):
Array (
[0] => Array (
[0] => stdClass Object (
[cto_office_id] => 1
[office_name] => Airtel India
[address_line1] => Bendoor well
[phone_office1] => 12345678912
[phone_office2] => 789451234512
[address_line2] => Kankanady Circle
[city] => Mangluru
[state] => Haryana
[pin_code] => 002
[country] => India
[web_url] => airte.co.in
[office_quote] => Office
[date_registered] => 2011-11-24 05:59:51
[bill_mail_id] => 15612561
[bill_mail_id_type] =>
[acc_status] => enabled
[comments] =>
[is_account_deleted] => 0
[account_deleted_date] => 0000-00-00 00:00:00
)
)
)
I tried this $data['phone_office1'], but that does not work.
You have an object that is nested inside two arrays. You can get it out like this:
$data[0][0]->phone_office1

Categories