SimpleXML and ArrayAccess (I'm about to punch my monitor!)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

SimpleXML and ArrayAccess (I'm about to punch my monitor!)

Post by Citizen »

Ok, here's my problem:

Code: Select all

$xml = new SimpleXMLElement($xml);
 $var = $xml->QBXMLMsgsRs->ClassQueryRs;
 $vars = print_r($var,true);
Returns:
SimpleXMLElement Object
(
[@attributes] => Array
(
[requestID] => Q2xhc3NRdWVyeXw1
[statusCode] => 0
[statusSeverity] => Info
[statusMessage] => Status OK
)

[ClassRet] => Array
(
[0] => SimpleXMLElement Object
(
[ListID] => 80000002-1241128424
[TimeCreated] => 2009-04-30T17:53:44-05:00
[TimeModified] => 2009-04-30T17:53:44-05:00
[EditSequence] => 1241128424
[Name] => D1
[FullName] => D1
[IsActive] => true
[Sublevel] => 0
)

[1] => SimpleXMLElement Object
(
[ListID] => 80000001-1241128320
[TimeCreated] => 2009-04-30T17:52:00-05:00
[TimeModified] => 2009-04-30T17:52:00-05:00
[EditSequence] => 1241128320
[Name] => K1
[FullName] => K1
[IsActive] => true
[Sublevel] => 0
)

[2] => SimpleXMLElement Object
(
[ListID] => 80000003-1241128452
[TimeCreated] => 2009-04-30T17:54:12-05:00
[TimeModified] => 2009-04-30T17:54:12-05:00
[EditSequence] => 1241128452
[Name] => S1
[FullName] => S1
[IsActive] => true
[Sublevel] => 0
)
)

)
However, this:

Code: Select all

$xml = new SimpleXMLElement($xml);
 $var = $xml->QBXMLMsgsRs->ClassQueryRs->ClassRet;
 $vars = print_r($var,true);
 
Returns this:
SimpleXMLElement Object
(
[ListID] => 80000002-1241128424
[TimeCreated] => 2009-04-30T17:53:44-05:00
[TimeModified] => 2009-04-30T17:53:44-05:00
[EditSequence] => 1241128424
[Name] => D1
[FullName] => D1
[IsActive] => true
[Sublevel] => 0
)
Now, my understanding is that I'm going to have to use ArrayAccess (which I've never done before) and the examples on the manual aren't helping at all. They all seem to be custom class oriented and not something I would just use on my simpleXML object. Any ideas?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: SimpleXML and ArrayAccess (I'm about to punch my monitor!)

Post by Darhazer »

Code: Select all

if (is_array($xml->QBXMLMsgsRs->ClassQueryRs->ClassRet)) {
  foreach ($xml->QBXMLMsgsRs->ClassQueryRs->ClassRet as $classRet) {
   // do whatever your want with $classRet
 }
}
Or maybe this will work to:

Code: Select all

for ($i=0; i < sizeof($xml->QBXMLMsgsRs->ClassQueryRs->ClassRet); $i++) {
 // do whatever you need with $xml->QBXMLMsgsRs->ClassQueryRs->ClassRet[$i]
}
Last edited by Darhazer on Sun Sep 20, 2009 12:49 pm, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: SimpleXML and ArrayAccess (I'm about to punch my monitor!)

Post by pickle »

Ya - SimpleXML is so tweaked to the nines, that simple manipulation becomes difficult. There's a note on the documentation page for children() that basically says you can't simply iterate over an object

Try:

Code: Select all

$xml->QBXMLMsgsRs->ClassQueryRs->ClassRet->children();
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply