RPGDXThe center of Indie-RPG gaming
Not logged in. [log in] [register]
 
 
Post new topic Reply to topic  
View previous topic - View next topic  
Author Message
XMark
Guitar playin' black mage


Joined: 30 May 2002
Posts: 870
Location: New Westminster, BC, Canada

PostPosted: Tue May 22, 2007 10:35 pm    Post subject: Dealing with sub-arrays [quote]

I'm working with an XML document in PHP, and the function that I'm using stores it into array format. The thing is, though. In this example.

Code:

<a>
<b>Some content</b>
</a>


It would be stored in the array like:

Code:

$array[a][b][value] = "Some content";


However, in this case:

Code:

<a>
<b>Some content</b>
<b>Some more content</b>
</a>


It would be stored like:
Code:

$array[a][b][0][value] = "Some content";
$array[a][b][1][value] = "Some more content";


Let's say I want to access all of the b's, but I don't know whether there will be one or more b's in the document. How do I loop through the array in a way that accommodates for both storage methods?
_________________
Mark Hall
Abstract Productions
I PLAYS THE MUSIC THAT MAKES THE PEOPLES FALL DOWN!
Back to top  
cowgod
Wandering Minstrel


Joined: 22 Nov 2005
Posts: 114
Location: Pittsburgh, USA

PostPosted: Wed May 23, 2007 3:50 pm    Post subject: [quote]

You might try using a foreach loop over $array[a][b], but I'm not sure how well that would work.
Back to top  
XMark
Guitar playin' black mage


Joined: 30 May 2002
Posts: 870
Location: New Westminster, BC, Canada

PostPosted: Wed May 23, 2007 4:07 pm    Post subject: [quote]

Yeah, that's what I'm doing but what ends up happening in case one is it looks into [value], while in the other case it looks at [0] and [1].
_________________
Mark Hall
Abstract Productions
I PLAYS THE MUSIC THAT MAKES THE PEOPLES FALL DOWN!
Back to top  
XMark
Guitar playin' black mage


Joined: 30 May 2002
Posts: 870
Location: New Westminster, BC, Canada

PostPosted: Wed May 23, 2007 5:07 pm    Post subject: [quote]

Aha!

Code:

if (isset($array['a']['b']['0'])) {
  foreach ($array['a']['b'] as $var) {
    dostuffwith ($var['value']);
  }
}
else dostuffwith($array['a']['b']['value']);

_________________
Mark Hall
Abstract Productions
I PLAYS THE MUSIC THAT MAKES THE PEOPLES FALL DOWN!
Back to top  
Bjorn
Demon Hunter


Joined: 29 May 2002
Posts: 1425
Location: Germany

PostPosted: Sat Jun 02, 2007 9:13 pm    Post subject: [quote]

I think I would prefer consistency, like even if there's only one element, still have it an array with the element at 0. Like "$array['a'][0]['b'][0]['value']" for your first example.
Back to top  
Post new topic Reply to topic Page 1 of 1 All times are GMT
 



Display posts from previous:   
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum