WebSphere Message Broker, Version 8.0.0.7 Operating Systems: AIX, HP-Itanium, Linux, Solaris, Windows, z/OS

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.

PHP supports the following syntax for building arrays:
$array[] = "aaa";
$array[] = "bbb";
PHP allows an object to function as an array, and you can use this capability to create repeating elements in a tree. For example:
$output_root->XMLNSC->a->b->c[] = $input_root->XMLNSC->a->b;
$output_root->XMLNSC->a->b->c[] = $input_root->XMLNSC->a->c;
The code shown above creates the following elements:
<a>
	<b>
		<c>
			... // contents of $input_root->XMLNSC->a->b
		</c>
		<c>
			... // contents of $input_root->XMLNSC->a->c
		</c>
	</b>
</a>
You can use the array operator in the path, as shown in the following example:
$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);
}
This example builds an output message with a repeating bit element, one for each item element in the input message. The content of each bit element is determined by the transformItem function, which refers to the item element as its parameter. The following sample shows an example of this syntax being used for message transformation:

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.

You can assign standard PHP arrays into an element tree as a way of building subtrees by using a very compact syntax. The following example shows how to build a repeating element from an array of values:
$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>
Notices | Trademarks | Downloads | Library | Support | Feedback

Copyright IBM Corporation 1999, 2016Copyright IBM Corporation 1999, 2016.

        
        Last updated:
        
        Last updated: 2016-05-23 14:46:34


Concept topicConcept topic | Version 8.0.0.7 | ac69003_