See information about the latest product version
Using PHP arrays with XML
PHP arrays are associative arrays (maps) but you can treat them as lists by adding values without keys.
$array[] = "aaa";
$array[] = "bbb";
$output_root->XMLNSC->a->b->c[] = $input_root->XMLNSC->a->b;
$output_root->XMLNSC->a->b->c[] = $input_root->XMLNSC->a->c;
<a>
<b>
<c>
... // contents of $input_root->XMLNSC->a->b
</c>
<c>
... // contents of $input_root->XMLNSC->a->c
</c>
</b>
</a>
$output_root->XMLNSC->a->b[]->c = $input_root->XMLNSC->a->b;
$output_root->XMLNSC->a->b[]->c = $input_root->XMLNSC->a->c;
to
create the following elements:<a>
<b>
<c>
... // contents of $input_root->XMLNSC->a->b
</c>
</b>
<b>
<c>
... // contents of $input_root->XMLNSC->a->c
</c>
</b>
</a>
The following example uses no array operators:
$output_root->XMLNSC->a->b->c = $input_root->XMLNSC->a->b;
$output_root->XMLNSC->a->b->c = $input_root->XMLNSC->a->c;
The example above produces the following XML code:
<a>
<b>
<c>
... // contents of $input_root->XMLNSC->a->c (overwriting previous)
</c>
</b>
</a>
You can also iterate a set of repeating elements by using a foreach loop, as shown in the following example:
foreach ($input_root->XMLNSC->doc->item as $item) {
$output_root->XMLNSC->msg->bit[] = $this->transformItem($item);
}
You can view information about samples only when you use the information center that is integrated with the WebSphere® Message Broker Toolkit or the online information center. You can run samples only when you use the information center that is integrated with the WebSphere Message Broker Toolkit.
$output_root->XMLNSC->doc->item[] = array('aaa', 'bbb', 'ccc');
This
code builds a tree with three item elements:<doc>
<item>aaa</item>
<item>bbb</item>
<item>ccc</item>
</doc>
Although the PHP array looks like a list, it is an associative array with the keys 0, 1, and 2. The following example shows how to assign key/value pairs into the element tree:
$output_root->XMLNSC->doc->item = array('book' => 'PHP',
'fruit' => 'apple',
'dog' => 'Spaniel' );
Without the [] operator on the item element, the keys in the array are used to name the child elements:
<doc>
<item>
<book>PHP</book>
<fruit>apple</fruit>
<dog>Spaniel</dog>
</item>
</doc>
You can also nest arrays to represent more complex structures. For example:
output_root->XMLNSC->doc->items =
array('book' => array('title' => 'PHP',
'author' => 'A N Other'),
'fruit' => 'apple',
'dog' => array('breed' => 'Spaniel',
'ears' => 'long') );
The preceding example produces the following XML code:
<doc>
<items>
<book>
<title>PHP</title>
<author>A N Other</author>
</book>
<fruit>apple</fruit>
<dog>
<breed>Spaniel</breed>
<ears>long</ears>
</dog>
</items>
</doc>