SimpleXML, trying to loop through elements and create associative array out of values

I can't get SimpleXML to work as I need it too. I want to create an associative array out of elements. Here's what xml looks like

<?xml version="1.0"?>
<RateV4Response>
<Package ID="1">
<ZipOrigination>11901</ZipOrigination>
<ZipDestination>91730</ZipDestination>
<Pounds>0</Pounds>
<Ounces>3.5</Ounces>
<Size>REGULAR</Size>
<Machinable>TRUE</Machinable>
<Zone>8</Zone>

<Postage CLASSID="3">
    <MailService>Express Mail</MailService>
    <Rate>28.00</Rate>
</Postage>
<Postage CLASSID="2">
    <MailService>Express Mail Hold For Pickup</MailService>
    <Rate>28.00</Rate>
</Postage>

MailService and Rate should be an array. This is my php:

$usps_response = new SimpleXMLElement($result);
foreach($usps_response->Package->Postage as $service)
    $data[] = array($service->MailService => $service->Rate);

I don't see an error here, but $data is 28 empty elements. I also tried this:

$data[] = array($service['MailService'] => $service['Rate']);

but that didn't help

If I assign just $service to $data[], then I see 28 elements with 3 sub-elements each (attributes, MailService and Rate).

What am I doing wrong?

Here are pics to help see the data

Answers


Seems odd you want a key like Express Mail Hold For Pickup in your array but here we go:

foreach($usps_response->Package->Postage as $service)
    $data[] = array((string)$service->MailService => (string)$service->Rate);

The XML posted here is not valid XML, so I have to assume that it is either incomplete or the postal service produced something that was not very useful. With a few changes, I was able to make it work. Please see http://www.laprbass.com/RAY_temp_user193.php

<?php // RAY_temp_user193.php
error_reporting(E_ALL);
echo '<pre>';

// TEST DATA
$xml = <<<ENDXML
<?xml version="1.0"?>
<RateV4Response>
<Package ID="1">
<ZipOrigination>11901</ZipOrigination>
<ZipDestination>91730</ZipDestination>
<Pounds>0</Pounds>
<Ounces>3.5</Ounces>
<Size>REGULAR</Size>
<Machinable>TRUE</Machinable>
<Zone>8</Zone>

<Postage CLASSID="3">
    <MailService>Express Mail</MailService>
    <Rate>28.00</Rate>
</Postage>
<Postage CLASSID="2">
    <MailService>Express Mail Hold For Pickup</MailService>
    <Rate>28.00</Rate>
</Postage>
</Package>
</RateV4Response>
ENDXML;

// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);
var_dump($obj);

Now what would you like to do with this object?


Need Your Help

What is the equivalent of “-” from bash in Windows batch?

windows bash batch-file cmd

Basically, I searched all over the Internet for an answer to this and couldn't find one that even skirted the question.

iphone - What is the purpose of NSNotification?

iphone nsnotifications nsnotificationcenter nsnotification

Can you please explain what is the purpose of the NSNotification, and what are some situations I could use it? Does a notification call all classes in the app, or does it call a specific class, by

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.