Match two arrays from a SOAP XML response in PHP - php

I searched around the internet in the last 2 days and I didn't found any solution to my problem,
I'm making a web application using PHP communicating with SOAP Web Services, and when I'm getting the respond of the Web Service I output the result in a few arrays, so now I want to match 1 array to another,
for example, I have array $company containing a list of 5 company locations, and then I have another array $rates containing a list of 37 rates and prices but I have to match each price in which location it belongs to, and I don't know how to do that.
I'm sorry for the long example I tried to make the shortest that I can, but I want you should understand what I mean
Array
(
[0] => SimpleXMLElement Object
(
[Car_AvailabilityReply] => SimpleXMLElement Object
(
[availabilityDetails] => SimpleXMLElement Object
(
[computeMarkups] => SimpleXMLElement Object
(
[actionRequestCode] => N
)
[rateClass] => SimpleXMLElement Object
(
[criteriaSetType] => COR
)
[companyLocationInfo] => Array
(
[0] => SimpleXMLElement Object
(
[carCompanyData] => SimpleXMLElement Object
(
[companyName] => AVIS
)
[rentalLocation] => SimpleXMLElement Object
(
[address] => SimpleXMLElement Object
(
[addressDetails] => SimpleXMLElement Object
(
[line1] => 420 EAST 90TH STREET
)
[locationDetails] => SimpleXMLElement Object
(
[name] => NYCC07
)
)
)
)
[1] => SimpleXMLElement Object
(
[carCompanyData] => SimpleXMLElement Object
(
[companyName] => AVIS
)
[rentalLocation] => SimpleXMLElement Object
(
[address] => SimpleXMLElement Object
(
[addressDetails] => SimpleXMLElement Object
(
[line1] => 310 EAST 64TH STREET
)
[locationDetails] => SimpleXMLElement Object
(
[name] => NYCC06
)
)
)
)
[2] => SimpleXMLElement Object
(
[carCompanyData] => SimpleXMLElement Object
(
[companyName] => AVIS
)
[rentalLocation] => SimpleXMLElement Object
(
[address] => SimpleXMLElement Object
(
[addressDetails] => SimpleXMLElement Object
(
[line1] => 68 EAST 11TH STREET
)
[locationDetails] => SimpleXMLElement Object
(
[name] => NYCC03
)
)
)
)
)
[rates] => Array
(
[0] => SimpleXMLElement Object
(
[vehicleTypeInfo] => SimpleXMLElement Object
(
[vehicleCharacteristic] => SimpleXMLElement Object
(
[vehicleRentalPrefType] => CCAR
)
)
[carCompanyData] => SimpleXMLElement Object
(
[companyName] => AVIS
)
[pickupDropoffLocations] => Array
(
[0] => SimpleXMLElement Object
(
[locationType] => PUP
[locationDescription] => SimpleXMLElement Object
(
[name] => NYCC07
)
)
[1] => SimpleXMLElement Object
(
[locationType] => DOL
[locationDescription] => SimpleXMLElement Object
(
[name] => NYCC07
)
)
)
[rateDetailsInfo] => SimpleXMLElement Object
(
[tariffInfo] => Array
(
[0] => SimpleXMLElement Object
(
[rateAmount] => 83.99
[rateCurrency] => USD
)
[1] => SimpleXMLElement Object
(
[rateAmount] => 100.68
[rateCurrency] => USD
)
)
)
)
[1] => SimpleXMLElement Object
(
[vehicleTypeInfo] => SimpleXMLElement Object
(
[vehicleCharacteristic] => SimpleXMLElement Object
(
[vehicleRentalPrefType] => CCAR
)
)
[carCompanyData] => SimpleXMLElement Object
(
[companyName] => AVIS
)
[pickupDropoffLocations] => Array
(
[0] => SimpleXMLElement Object
(
[locationType] => PUP
[locationDescription] => SimpleXMLElement Object
(
[name] => NYCC06
)
)
[1] => SimpleXMLElement Object
(
[locationType] => DOL
[locationDescription] => SimpleXMLElement Object
(
[name] => NYCC06
)
)
)
[rateDetailsInfo] => SimpleXMLElement Object
(
[tariffInfo] => Array
(
[0] => SimpleXMLElement Object
(
[rateAmount] => 83.99
[rateCurrency] => USD
)
[1] => SimpleXMLElement Object
(
[rateAmount] => 100.68
[rateCurrency] => USD
)
)
)
)
[2] => SimpleXMLElement Object
(
[vehicleTypeInfo] => SimpleXMLElement Object
(
[vehicleCharacteristic] => SimpleXMLElement Object
(
[vehicleRentalPrefType] => CCAR
)
)
[carCompanyData] => SimpleXMLElement Object
(
[companyName] => AVIS
)
[pickupDropoffLocations] => Array
(
[0] => SimpleXMLElement Object
(
[locationType] => PUP
[locationDescription] => SimpleXMLElement Object
(
[name] => NYCC03
)
)
[1] => SimpleXMLElement Object
(
[locationType] => DOL
[locationDescription] => SimpleXMLElement Object
(
[name] => NYCC03
)
)
)
[rateDetailsInfo] => SimpleXMLElement Object
(
[tariffInfo] => Array
(
[0] => SimpleXMLElement Object
(
[rateAmount] => 83.99
[rateCurrency] => USD
)
[1] => SimpleXMLElement Object
(
[rateAmount] => 100.68
[rateCurrency] => USD
)
)
)
)
[3] => SimpleXMLElement Object
(
[vehicleTypeInfo] => SimpleXMLElement Object
(
[vehicleCharacteristic] => SimpleXMLElement Object
(
[vehicleRentalPrefType] => EDAR
)
)
[carCompanyData] => SimpleXMLElement Object
(
[companyName] => AVIS
)
[pickupDropoffLocations] => Array
(
[0] => SimpleXMLElement Object
(
[locationType] => PUP
[locationDescription] => SimpleXMLElement Object
(
[name] => NYCC03
)
)
[1] => SimpleXMLElement Object
(
[locationType] => DOL
[locationDescription] => SimpleXMLElement Object
(
[name] => NYCC03
)
)
)
[rateDetailsInfo] => SimpleXMLElement Object
(
[tariffInfo] => Array
(
[0] => SimpleXMLElement Object
(
[rateAmount] => 83.99
[rateCurrency] => USD
)
[1] => SimpleXMLElement Object
(
[rateAmount] => 100.68
[rateCurrency] => USD
)
)
)
)
[4] => SimpleXMLElement Object
(
[vehicleTypeInfo] => SimpleXMLElement Object
(
[vehicleCharacteristic] => SimpleXMLElement Object
(
[vehicleRentalPrefType] => EDAR
)
)
[carCompanyData] => SimpleXMLElement Object
(
[companyName] => AVIS
)
[pickupDropoffLocations] => Array
(
[0] => SimpleXMLElement Object
(
[locationType] => PUP
[locationDescription] => SimpleXMLElement Object
(
[name] => NYCC06
)
)
[1] => SimpleXMLElement Object
(
[locationType] => DOL
[locationDescription] => SimpleXMLElement Object
(
[name] => NYCC06
)
)
)
[rateDetailsInfo] => SimpleXMLElement Object
(
[tariffInfo] => Array
(
[0] => SimpleXMLElement Object
(
[rateAmount] => 83.99
[rateCurrency] => USD
)
[1] => SimpleXMLElement Object
(
[rateAmount] => 100.68
[rateCurrency] => USD
)
)
)
)
)
)
)
)
)
If you understand that you can see there is two type of arrays the first 3 is the location and the second is the cars with price, and I have to put each car to the correct location.
If you need more explanation please tell me.
Please help me out with that,
Thank you.

Related

SimpleXMLElement Object response for loop

Im trying to return a list of contacts from the Xero API however when getting the response back from there api i cannot run the array through a for each loop.
when trying to loop through nothing is displayed, has anyone had this problem in the past?
im trying to loop through to put each contact within a row inside a table.
if i run
pr($contacts->Contacts[0]);
it returns the list of contacts however it wont display anything with the for loop below
This is my for loop
foreach($contacts->Contacts[0] as $item) {
echo $item['Id'];
echo $item['filepath'];
}
this is the array returned
SimpleXMLElement Object
(
[Id] => 0dcda838-d354-4674-b6eb-d788e166b8dd
[Status] => OK
[ProviderName] => TestIntergration
[DateTimeUTC] => 2017-09-18T01:42:30.7361585Z
[Contacts] => SimpleXMLElement Object
(
[Contact] => Array
(
[0] => SimpleXMLElement Object
(
[ContactID] => 0ae2c0f4-cae9-4431-ab6b-52554f1c7c69
[ContactStatus] => ACTIVE
[Name] => Steve Buscemi
[Addresses] => SimpleXMLElement Object
(
[Address] => Array
(
[0] => SimpleXMLElement Object
(
[AddressType] => STREET
)
[1] => SimpleXMLElement Object
(
[AddressType] => POBOX
)
)
)
[Phones] => SimpleXMLElement Object
(
[Phone] => Array
(
[0] => SimpleXMLElement Object
(
[PhoneType] => DDI
)
[1] => SimpleXMLElement Object
(
[PhoneType] => DEFAULT
)
[2] => SimpleXMLElement Object
(
[PhoneType] => FAX
)
[3] => SimpleXMLElement Object
(
[PhoneType] => MOBILE
)
)
)
[UpdatedDateUTC] => 2017-08-30T08:14:03.88
[IsSupplier] => false
[IsCustomer] => true
[HasAttachments] => false
)
[1] => SimpleXMLElement Object
(
[ContactID] => ab8a9286-740e-4246-876c-9fcac558d600
[ContactStatus] => ACTIVE
[Name] => Martin Hudson
[Addresses] => SimpleXMLElement Object
(
[Address] => Array
(
[0] => SimpleXMLElement Object
(
[AddressType] => STREET
)
[1] => SimpleXMLElement Object
(
[AddressType] => POBOX
)
)
)
[Phones] => SimpleXMLElement Object
(
[Phone] => Array
(
[0] => SimpleXMLElement Object
(
[PhoneType] => DDI
)
[1] => SimpleXMLElement Object
(
[PhoneType] => DEFAULT
)
[2] => SimpleXMLElement Object
(
[PhoneType] => FAX
)
[3] => SimpleXMLElement Object
(
[PhoneType] => MOBILE
)
)
)
[UpdatedDateUTC] => 2017-08-30T08:20:03.263
[IsSupplier] => false
[IsCustomer] => true
[HasAttachments] => false
)
[2] => SimpleXMLElement Object
(
[ContactID] => 463e9f23-d1a1-4d74-b19e-041142c8fd34
[ContactStatus] => ACTIVE
[Name] => Orlena Greenville
[Addresses] => SimpleXMLElement Object
(
[Address] => Array
(
[0] => SimpleXMLElement Object
(
[AddressType] => STREET
)
[1] => SimpleXMLElement Object
(
[AddressType] => POBOX
)
)
)
[Phones] => SimpleXMLElement Object
(
[Phone] => Array
(
[0] => SimpleXMLElement Object
(
[PhoneType] => DDI
)
[1] => SimpleXMLElement Object
(
[PhoneType] => DEFAULT
)
[2] => SimpleXMLElement Object
(
[PhoneType] => FAX
)
[3] => SimpleXMLElement Object
(
[PhoneType] => MOBILE
)
)
)
[UpdatedDateUTC] => 2017-09-01T07:56:32.667
[IsSupplier] => false
[IsCustomer] => false
[HasAttachments] => false
)
[3] => SimpleXMLElement Object
(
[ContactID] => b75bfe5b-cc6e-45ee-b0a1-feafe866088d
[ContactStatus] => ACTIVE
[Name] => asdfasdfasdf454
[Addresses] => SimpleXMLElement Object
(
[Address] => Array
(
[0] => SimpleXMLElement Object
(
[AddressType] => STREET
)
[1] => SimpleXMLElement Object
(
[AddressType] => POBOX
)
)
)
[Phones] => SimpleXMLElement Object
(
[Phone] => Array
(
[0] => SimpleXMLElement Object
(
[PhoneType] => DDI
)
[1] => SimpleXMLElement Object
(
[PhoneType] => DEFAULT
)
[2] => SimpleXMLElement Object
(
[PhoneType] => FAX
)
[3] => SimpleXMLElement Object
(
[PhoneType] => MOBILE
)
)
)
[UpdatedDateUTC] => 2017-09-04T10:34:52.93
[IsSupplier] => false
[IsCustomer] => false
[HasAttachments] => false
)
)
)
)
Code used to get the passed data
$response = $XeroOAuth->request('GET', $XeroOAuth->url('Contacts', 'core'), array());
if ($XeroOAuth->response['code'] == 200) {
$contacts = $XeroOAuth->parseResponse($XeroOAuth->response['response'], $XeroOAuth->response['format']);
pr($contacts);
}

PHP - Dealing with a Indexed Arrays

I have an array of arrays that I'm finding trouble looping with foreach and getting accessing the data.
They array key has dates, then the array inside this has badly ordered indexes.
array(
[2016-11-03T07:00:00] => Array
(
[0] => stdClass Object
(
[Job] => stdClass Object
(
[ID] => 87126387136
[Foo] => stdClass Object
(
[ID] => 0
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
[Premises] => stdClass Object
(
[Location] => stdClass Object
(
[Metric] => stdClass Object
(
[Latitude] => 51.3453534534
[Longitude] => -0.417308
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[ScheduledStart] => 2016-11-03T07:00:00
)
)
[6] => stdClass Object
(
[Job] => stdClass Object
(
[ID] => 234923749
[Foo] => stdClass Object
(
[ID] => 0
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
[Premises] => stdClass Object
(
[Location] => stdClass Object
(
[Metric] => stdClass Object
(
[Latitude] => 51.34535435345
[Longitude] => -0.417308
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[ScheduledStart] => 2016-11-03T07:00:00
)
)
)
[2016-11-24T07:00:00] => Array
(
[3] => stdClass Object
(
[Job] => stdClass Object
(
[ID] => 3749823749
[Foo] => stdClass Object
(
[ID] => 0
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
[Premises] => stdClass Object
(
[Location] => stdClass Object
(
[Metric] => stdClass Object
(
[Latitude] => 51.345345435
[Longitude] => -0.417308
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[ScheduledStart] => 2016-11-24T07:00:00
)
)
[5] => stdClass Object
(
[Job] => stdClass Object
(
[ID] => 4792874923874
[Foo] => stdClass Object
(
[ID] => 0
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
[Premises] => stdClass Object
(
[Location] => stdClass Object
(
[Metric] => stdClass Object
(
[Latitude] => 51.3453453444
[Longitude] => -0.417308
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[ScheduledStart] => 2016-11-24T07:00:00
)
)
[9] => stdClass Object
(
[Job] => stdClass Object
(
[ID] => 9943386
[Foo] => stdClass Object
(
[ID] => 0
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
[Premises] => stdClass Object
(
[Location] => stdClass Object
(
[Metric] => stdClass Object
(
[Latitude] => 51.34534534534
[Longitude] => -0.417308
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[ScheduledStart] => 2016-11-24T07:00:00
)
)
)
)
Is it possible to loop through this array using foreach in it's current state?
I'm open to suggestion of manipulating the array if for a more efficient solution.
There are two layers of arrays - the first with dates as indices, the second with numbers as indices. Values of the second arrays are nested objects.
foreach ($arr as $date => $v) {
echo "date: ".$date."<br>";
foreach ($v as $number => $job) {
echo "number: ".$number."<br>";
echo $job->ID."<br>";
echo $job->Foo->ID."<br>";
echo $job->Foo->RecordStamp->AddedBy."<br>";
echo $job->Premises->Location->Metric->Latitude."<br>";
echo $job->ScheduledStart."<br>";
}
}

Multidimensional array in PHP?

How to read specific values from multi dimensional array using php below is the array. I want to read values of [itemBody][div][0][p].
SimpleXMLElement Object
(
[#attributes] => Array
(
[toolName] => Eqiat
[toolVersion] => 0.7~git
[adaptive] => false
[timeDependent] => false
[identifier] => ITEM_b95e4391e33aa28561b01493b6e328f8
[title] => match the following
)
[stylesheet] => SimpleXMLElement Object
(
[#attributes] => Array
(
[href] => http://gauss.ecs.soton.ac.uk/eqiat/eqiat.css
[type] => text/css
[title] => Eqiat item styles, designed to override QTIEngine's output where appropriate
)
)
[responseDeclaration] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_0
[cardinality] => multiple
[baseType] => identifier
)
[correctResponse] => SimpleXMLElement Object
(
[value] => question_0_option_3
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_1
[cardinality] => multiple
[baseType] => identifier
)
[correctResponse] => SimpleXMLElement Object
(
[value] => question_1_option_2
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_2
[cardinality] => multiple
[baseType] => identifier
)
[correctResponse] => SimpleXMLElement Object
(
[value] => question_2_option_1
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_3
[cardinality] => multiple
[baseType] => identifier
)
[correctResponse] => SimpleXMLElement Object
(
[value] => question_3_option_0
)
)
)
[outcomeDeclaration] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
[cardinality] => single
[baseType] => integer
)
[defaultValue] => SimpleXMLElement Object
(
[value] => 0
)
)
[itemBody] => SimpleXMLElement Object
(
[div] => Array
(
[0] => SimpleXMLElement Object
(
[p] => Match The Following
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[class] => eqiat-emi
)
[ol] => SimpleXMLElement Object
(
[#attributes] => Array
(
[class] => emioptions
)
[li] => Array
(
[0] => bangalore
[1] => india
[2] => tiger
[3] => onion
)
)
[choiceInteraction] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[maxChoices] => 0
[minChoices] => 0
[shuffle] => false
[responseIdentifier] => RESPONSE_question_0
)
[prompt] => vegetable
[simpleChoice] => Array
(
[0] => A
[1] => B
[2] => C
[3] => D
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[maxChoices] => 0
[minChoices] => 0
[shuffle] => false
[responseIdentifier] => RESPONSE_question_1
)
[prompt] => animal
[simpleChoice] => Array
(
[0] => A
[1] => B
[2] => C
[3] => D
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[maxChoices] => 0
[minChoices] => 0
[shuffle] => false
[responseIdentifier] => RESPONSE_question_2
)
[prompt] => country
[simpleChoice] => Array
(
[0] => A
[1] => B
[2] => C
[3] => D
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[maxChoices] => 0
[minChoices] => 0
[shuffle] => false
[responseIdentifier] => RESPONSE_question_3
)
[prompt] => city
[simpleChoice] => Array
(
[0] => A
[1] => B
[2] => C
[3] => D
)
)
)
)
)
)
[responseProcessing] => SimpleXMLElement Object
(
[setOutcomeValue] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
[baseValue] => 0
)
[responseCondition] => Array
(
[0] => SimpleXMLElement Object
(
[responseIf] => SimpleXMLElement Object
(
[match] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_0
)
)
[correct] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_0
)
)
)
[setOutcomeValue] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
[sum] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
)
[baseValue] => 1
)
)
)
)
[1] => SimpleXMLElement Object
(
[responseIf] => SimpleXMLElement Object
(
[match] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_1
)
)
[correct] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_1
)
)
)
[setOutcomeValue] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
[sum] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
)
[baseValue] => 1
)
)
)
)
[2] => SimpleXMLElement Object
(
[responseIf] => SimpleXMLElement Object
(
[match] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_2
)
)
[correct] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_2
)
)
)
[setOutcomeValue] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
[sum] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
)
[baseValue] => 1
)
)
)
)
[3] => SimpleXMLElement Object
(
[responseIf] => SimpleXMLElement Object
(
[match] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_3
)
)
[correct] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => RESPONSE_question_3
)
)
)
[setOutcomeValue] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
[sum] => SimpleXMLElement Object
(
[variable] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => SCORE
)
)
[baseValue] => 1
)
)
)
)
)
)
)
Try:
$element = $object->itemBody->div->0->p;
That is an object and itemBody is a property that holds another object with a property div which is an array of objects with properties, p being one for the object at array index 0:
$result = $object->itemBody->div[0]->p;
First convert the complete object in to array and then access
' function object2array($object) { '
return json_decode(json_encode($object), true);
}
$data = object2array(simplexml_load_string($xml1));
echo '',print_r($data),'';`
Now i can access by echo $data[itemBody][div][0][p];

SimpleXMLElement To PHP Variables

I have been trying to get the Amount from the LandedPrice area of this XML that was returned. I have tried this and other code that looks similar. Would anyone have any suggestions of how I would grab that information from this?
$simplexml = new SimpleXMLElement($response); // Is response from server.
$amount = (string) $simplexml->CompetitivePrice[0]->LandedPrice->Price->Amount;
SimpleXMLElement Object
( [GetCompetitivePricingForASINResult] => SimpleXMLElement Object
( [#attributes] => Array ( [ASIN] => B00H9M9B34 [status] => Success ) [Product] => SimpleXMLElement Object
( [Identifiers] => SimpleXMLElement Object
( [MarketplaceASIN] => SimpleXMLElement Object
( [MarketplaceId] => ATVPDKIKX0DER [ASIN] => B00H9M9B34 )
) [CompetitivePricing] => SimpleXMLElement Object
( [CompetitivePrices] => SimpleXMLElement Object
( [CompetitivePrice] => SimpleXMLElement Object
( [#attributes] => Array ( [belongsToRequester] => false [condition] => New [subcondition] => New ) [CompetitivePriceId] => 1 [Price] => SimpleXMLElement Object
( [LandedPrice] => SimpleXMLElement Object
( [CurrencyCode] => USD [Amount] => 75.00 ) [ListingPrice] => SimpleXMLElement Object
( [CurrencyCode] => USD [Amount] => 75.00 ) [Shipping] => SimpleXMLElement Object
( [CurrencyCode] => USD [Amount] => 0.00 )
)
)
) [NumberOfOfferListings] => SimpleXMLElement Object
( [OfferListingCount] => Array ( [0] => 1 [1] => 1 ) )
) [SalesRankings] => SimpleXMLElement Object
( [SalesRank] => SimpleXMLElement Object
( [ProductCategoryId] => sports_display_on_website [Rank] => 625877 )
)
)
)
[ResponseMetadata] => SimpleXMLElement Object
( [RequestId] => 234234343d-bawff3f3-vef5555rrer)
)

Looping XML Values

I exported the .kml file for a place list created on google maps, this looks like so:
<Placemark>
<name>Name of first business here</name>
<description><![CDATA[<div dir="ltr">Address etc
<br>Tel No
<br>www.example.com</div>]]></description>
<styleUrl>#style17</styleUrl>
<Point>
<coordinates>1893891893891</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Second business</name>
<description><![CDATA[<div dir="ltr">Address here
<br>Tel no
<br>www.webaddress.com</div>]]></description>
<styleUrl>#style5</styleUrl>
<Point>
<coordinates>7317783871871</coordinates>
</Point>
</Placemark>
<Placemark>
..
</Placemark>
..
I would simply like to loop through and echo the names out to the page.
I have tried loading the file as a simplexml file and using a foreach loop to attempt to read the values, with no success.
This code prints out just the first name, doesn't appear to loop, stops after first name:
<?php
$distr = simplexml_load_file('../../media/Distributors.kml');
foreach ($distr as $value) {
echo '<li>' . $value->Placemark->name . '</li>';
}
?>
This code looks like it should work, but prints nothing:
<?php
$distr = simplexml_load_file('../../media/Distributors.kml');
foreach ($distr->Placemark as $placemark) {
echo '<li>' . $placemark->name . '</li>';
}
?>
I've tried a few other combinations but neither seem to do what I want, what am I doing wrong?
Print_r dump:
SimpleXMLElement Object
(
[Document] => SimpleXMLElement Object
(
[name] => Distributors
[description] => SimpleXMLElement Object
(
)
[Style] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style17
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => http://maps.gstatic.com/mapfiles/ms2/micons/blue-dot.png
)
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style5
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => http://maps.gstatic.com/mapfiles/ms2/micons/blue-dot.png
)
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style4
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style18
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => http://maps.gstatic.com/mapfiles/ms2/micons/blue-dot.png
)
)
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style15
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[5] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style20
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[6] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style6
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[7] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style23
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[8] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style22
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[9] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style11
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[10] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style7
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[11] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style12
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[12] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style3
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
[22] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => style8
)
[IconStyle] => SimpleXMLElement Object
(
[Icon] => SimpleXMLElement Object
(
[href] => SimpleXMLElement Object
(
)
)
)
)
)
[Placemark] => Array
(
[0] => SimpleXMLElement Object
(
[name] => Rigibore Ltd
[description] => SimpleXMLElement Object
(
)
[styleUrl] => #style17
[Point] => SimpleXMLElement Object
(
[coordinates] => -5.399673,50.191875,0.000000
)
)
[1] => SimpleXMLElement Object
(
[name] => Rigibore Inc.
[description] => SimpleXMLElement Object
(
)
[styleUrl] => #style5
[Point] => SimpleXMLElement Object
(
[coordinates] => -88.320183,42.867550,0.000000
)
)
[2] => SimpleXMLElement Object
(
[name] => Vibha Technologies
[styleUrl] => #style4
[Point] => SimpleXMLElement Object
(
[coordinates] => 77.568222,13.002831,0.000000
)
)
[3] => SimpleXMLElement Object
(
[name] => Centra-Tech bvba
[description] => SimpleXMLElement Object
(
)
[styleUrl] => #style18
[Point] => SimpleXMLElement Object
(
[coordinates] => 4.867222,51.141304,0.000000
)
)
[4] => SimpleXMLElement Object
(
[name] => A B T Machine Tools & Tooling Ltd
[styleUrl] => #style15
[Point] => SimpleXMLElement Object
(
[coordinates] => -4.324394,55.894394,0.000000
)
)
[5] => SimpleXMLElement Object
(
[name] => Cromwell Tools Ltd WDC
[styleUrl] => #style20
[Point] => SimpleXMLElement Object
(
[coordinates] => -1.124264,52.585289,0.000000
)
)
[6] => SimpleXMLElement Object
(
[name] => Beijing Promise
[description] => SimpleXMLElement Object
(
)
[styleUrl] => #style6
[Point] => SimpleXMLElement Object
(
[coordinates] => 116.300972,39.945293,0.000000
)
)
[7] => SimpleXMLElement Object
(
[name] => Donini E Grandi Srl Do-Gra
[styleUrl] => #style23
[Point] => SimpleXMLElement Object
(
[coordinates] => 11.331189,44.512886,0.000000
)
)
[8] => SimpleXMLElement Object
(
[name] => Euskron S.A.
[styleUrl] => #style22
[Point] => SimpleXMLElement Object
(
[coordinates] => -1.947317,43.093163,0.000000
)
)
[9] => SimpleXMLElement Object
(
[name] => H B Tools Ltd
[styleUrl] => #style11
[Point] => SimpleXMLElement Object
(
[coordinates] => -2.865015,53.601593,0.000000
)
)
[10] => SimpleXMLElement Object
(
[name] => Monks & Crane Ltd
[styleUrl] => #style7
[Point] => SimpleXMLElement Object
(
[coordinates] => -2.028771,52.547211,0.000000
)
)
[11] => SimpleXMLElement Object
(
[name] => Hudson Foster
[description] => SimpleXMLElement Object
(
)
[styleUrl] => #style12
[Point] => SimpleXMLElement Object
(
[coordinates] => -1.509982,53.665184,0.000000
)
)
[12] => SimpleXMLElement Object
(
[name] => BIM Sp. z o.o.
[description] => SimpleXMLElement Object
(
)
[styleUrl] => #style3
[Point] => SimpleXMLElement Object
(
[coordinates] => 16.772060,52.437359,0.000000
)
)
[13] => SimpleXMLElement Object
(
[name] => Precise Tooling System
[description] => SimpleXMLElement Object
(
)
[styleUrl] => #style9
[Point] => SimpleXMLElement Object
(
[coordinates] => 103.875557,1.377493,0.000000
)
)
[14] => SimpleXMLElement Object
(
[name] => Campbell Peter Sales Ltd
[styleUrl] => #style2
[Point] => SimpleXMLElement Object
(
[coordinates] => -3.949751,55.848644,0.000000
)
)
[15] => SimpleXMLElement Object
(
[name] => Obradors D R M, S.l.
[styleUrl] => #style16
[Point] => SimpleXMLElement Object
(
[coordinates] => 2.171534,41.400570,0.000000
)
)
[16] => SimpleXMLElement Object
(
[name] => Multicarb CC T/A Multitrade Distributors
[styleUrl] => #style1
[Point] => SimpleXMLElement Object
(
[coordinates] => 28.178808,-26.152639,0.000000
)
)
[17] => SimpleXMLElement Object
(
[name] => Howard Lee & Son
[styleUrl] => #style10
[Point] => SimpleXMLElement Object
(
[coordinates] => -2.062812,52.471966,0.000000
)
)
[18] => SimpleXMLElement Object
(
[name] => Glendower Cutting Tools Ltd
[styleUrl] => #style21
[Point] => SimpleXMLElement Object
(
[coordinates] => -1.105845,52.674934,0.000000
)
)
[19] => SimpleXMLElement Object
(
[name] => Finn A Værktøj A/S
[styleUrl] => #style13
[Point] => SimpleXMLElement Object
(
[coordinates] => 9.853188,54.986698,0.000000
)
)
[20] => SimpleXMLElement Object
(
[name] => Flatley.PMG
[styleUrl] => #style14
[Point] => SimpleXMLElement Object
(
[coordinates] => -6.337717,53.312447,0.000000
)
)
[21] => SimpleXMLElement Object
(
[name] => Iscar Tools Argentina Sa
[styleUrl] => #style19
[Point] => SimpleXMLElement Object
(
[coordinates] => -58.404968,-34.639957,0.000000
)
)
[22] => SimpleXMLElement Object
(
[name] => Duroc Machine Tool AB
[styleUrl] => #style8
[Point] => SimpleXMLElement Object
(
[coordinates] => 18.092628,59.449158,0.000000
)
)
)
)
)
If you want to read xml file and loop through the xml elments .try something like this
<songs>
<song dateplayed="2011-07-24 19:40:26">
<title>I left my heart on Europa</title>
<artist>Ship of Nomads</artist>
</song>
<song dateplayed="2011-07-24 19:27:42">
<title>Oh Ganymede</title>
<artist>Beefachanga</artist>
</song>
<song dateplayed="2011-07-24 19:23:50">
<title>Kallichore</title>
<artist>Jewitt K. Sheppard</artist>
</song>
</songs>
<?php
$dir = "home/";
$files = glob($dir."*.xml");
foreach($files as $file_xml)
{
$headernode = new SimpleXMLElement($file_xml,null,true);
foreach($headernode as $property)
{
echo $name = $property->first;
}
}
OR try something like this
$mysongs = simplexml_load_file('songs.xml');
echo $mysongs->song[0]->artist;
?>

Categories