From: Brian Dunning on
I am kind of jacked here. I have a SimpleXML object that's been converted to an array. In one of the nodes, for no reason I can see, the array is populated differently if there is only one <order_item> than if there are multiple <order_item>s.

If there is just one, my $order_item array looks like this:
Array
(
[order_item_product_code] => 1300
[order_item_description] => 4x8 photo cards (set of 20)
)

But if there are multiple items, I get this:
Array
(
[0] => SimpleXMLElement Object
(
[order_item_product_code] => 1060
[order_item_description] => 4x5.3 trueDigital print(s)
)

[1] => SimpleXMLElement Object
(
[order_item_product_code] => 1300
[order_item_description] => 4x8 photo cards (set of 20)
)
)

See my problem? I can't foreach through those and process the items in the same way.

In case it matters, here is that snip of what the original XML looks like, it's all good:
<?xml version="1.0" encoding="UTF-8"?>
<order xmlns="http://xxxx.com/xxxx" version="1.0">
<order_items>
<order_item>
<order_item_product_code finish="glossy">1060</order_item_product_code>
<order_item_description>4x5.3 trueDigital print(s)</order_item_description>
</order_item>
<order_item>
<order_item_product_code finish="glossy">1300</order_item_product_code>
<order_item_description>4x8 photo cards (set of 20)</order_item_description>
</order_item>
</order_items>
</order>

And here is the function I'm using to convert the XML to an array:
function xmlToArray($xml, $isChild = false) {
if($xml instanceof SimpleXMLElement) {
$xmlObject = $xml;
} elseif($isChild) {
return $xml;
} else {
try {
$xmlObject = new SimpleXMLElement($xml);
} catch(Exception $e) {
$string = $e->getMessage();
throw new Exception('Invalid XML found in ' . __FUNCTION__ . ' function on line '.__LINE__.'.');
}
}
$children = (array)$xmlObject->children();
if(is_array($children)) {
if(count($children) == 0) {
$children = '';
} else {
foreach($children as $element => $value) {
$children[$element] = xmlToArray($value, true);
}
}
}
return $children;
}

Any suggestions?
From: Nathan Nobbe on
On Tue, Sep 28, 2010 at 4:29 PM, Brian Dunning <brian(a)briandunning.com>wrote:

> I am kind of jacked here. I have a SimpleXML object that's been converted
> to an array.


how was the SimpleXMLElement converted to an array?

-nathan
From: Brian Dunning on
If you read down to the bottom of the post, the function I used is given.


On Sep 28, 2010, at 3:47 PM, Nathan Nobbe wrote:

> On Tue, Sep 28, 2010 at 4:29 PM, Brian Dunning <brian(a)briandunning.com> wrote:
> I am kind of jacked here. I have a SimpleXML object that's been converted to an array.
>
> how was the SimpleXMLElement converted to an array?
>
> -nathan

From: Nathan Nobbe on
On Tue, Sep 28, 2010 at 5:06 PM, Brian Dunning <brian(a)briandunning.com>wrote:

> If you read down to the bottom of the post, the function I used is given.


My apologies for the hasty response. well, if i had to guess, id say thats
where the problem is. why cant you just iterate over the object w/ the
built in functionality, which is to say, why bother converting to an array
in the first place?

-nathan
From: "Erik L. Arneson" on
On Tue, 28 Sep 2010, Brian Dunning wrote:
> I am kind of jacked here. I have a SimpleXML object that's been
> converted to an array. In one of the nodes, for no reason I can see,
> the array is populated differently if there is only one <order_item>
> than if there are multiple <order_item>s.
>
> [...]
>
> In case it matters, here is that snip of what the original XML looks
> like, it's all good:
> <?xml version="1.0" encoding="UTF-8"?>
> <order xmlns="http://xxxx.com/xxxx" version="1.0">
> <order_items>
> <order_item>
> <order_item_product_code finish="glossy">1060</order_item_product_code>
> <order_item_description>4x5.3 trueDigital print(s)</order_item_description>
> </order_item>
> <order_item>
> <order_item_product_code finish="glossy">1300</order_item_product_code>
> <order_item_description>4x8 photo cards (set of 20)</order_item_description>
> </order_item>
> </order_items>
> </order>

What does the XML look like if there's only one order item? Maybe
you're losing a level of nesting there?

--
Erik Arneson <dybbuk(a)LNouv.com>
GPG Key ID : 1024D/62DA1D25 BitCoin : 1LqvuGUqJ4ZUSoE7YE9ngETjwp4yZ2uSdP
Office : +1.541.291.9776 Skype : callto://pymander
http://www.leisurenouveau.com/