How to pass and read an array of an array in PHP?

Jesse Allen

New member
Hopefully someone can help me here, it's kind of complicated. I'm trying to pass data from a form to an XML doc. Part of the form allows multiple contributors to be appended. In the XML I'd like it to look like:

<contributors>
<contributor>
<name></name>
<role></role>
<association></association>
</contributor>
...
</contributors>

And my form is set up more or less like this in HTML:

<input name="contributor[name[]]" />
<input name="contributor[role[]]" />
<input name="contributor[association[]]" />

The data passed just fine when the page was set up like this:

<input name="contributorName[]" />

But the data came in the order name1, name2, role1, role2, etc...
It seems like doing an array of an array should solve the ordering problem but I think I either forgot how to properly write PHP or it's just really late. Here is the PHP I have written relative to this piece:

// Collect information from POST request
foreach($_POST as $key => $value) {
if(is_array($value)) {
// Special Case for contributors
if($key == "contributor") {
$event->addChild($key);
for($i = 0; $i < count($value); $i++) {
$c = "contributor";
$event->$key->addChild("contributor");
$event->$key->$c->addChild("name", $value[name[$i]]);
$event->$key->$c->addChild("role", $value[role[$i]]);
$event->$key->$c->addChild("affiliation", $value[affiliation[$i]]);
}
}
// If it's just a single level array set
else
{
$event->addChild($key."Set");
for($i = 0; $i < count($value); $i++) {
$event->$key->addChild($key, $value[$i]);
}
}
// If it's not an array, just add it
} else {
$event->addChild($key, $value);
}
}

Here is the kind of error I got:

Parse error: syntax error, unexpected '[', expecting ']' in /nfs/aesop01/hw12/d22/codexa/process.php on line 37

It's obviously something about the double level array but it seems like it should work. If anyone has had similar problems and/or found solutions I would like to hear them! Thanks.
 
Back
Top