Wraps the results of an XPath query in a CFArray
object.
Access
public
Parameters
Parameter |
Type |
Required |
Description |
---|---|---|---|
|
Required |
The XPath expression to use to query the XML response. |
Returns
Examples
New additions in CFArray and CFSimpleXML.
A fairly contrived example showing how to use new additions in CFArray and CFSimpleXML.
This example uses anonymous functions, so it will only work as-is with PHP 5.3+. PHP 5.2.x users can refactor this example to use standard functions instead.
$ec2 = new AmazonEC2(); $response = $ec2->describe_images(); // Use an XPath query to find a matching set of nodes. $list = $response->body->query('descendant-or-self::item[imageId[contains(., "aki-")] and architecture = "x86_64" and imageOwnerAlias = "amazon"]/imageLocation'); // CFArray::map() works similarly to array_map(). $results = $list->map(function($node) { return $node->parent(); }); // Manually instantiate CFArray. $image_locations = new CFArray(array()); /* For each entry in the $results array, pass it to the anonymous function. You can bind a variable (e.g., $image_locations) to the inner scope by using pass-by-reference. */ $results->each(function($node, $i, &$image_locations) { $image_locations[] = (string) $node->imageLocation; }, $image_locations); // Display the first and last entries in the resulting array. echo $image_locations->first() . PHP_EOL; echo $image_locations->last() . PHP_EOL;Result:
ec2-paid-ibm-images-ids/vmlinuz-2.6.16.60-0.30-xen.x86_64.manifest.xml ec2-paid-ibm-images-db2/vmlinuz-2.6.16.60-0.30-xen.x86_64.manifest.xml
Source
Method defined in utilities/simplexml.class.php | Toggle source view (4 lines) | View on GitHub