first ()

Gets the first result in the array.

Access

public

Returns

Type

Description

mixed

The first result in the CFArray object. Returns false if there are no items in the array.

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/array.class.php | Toggle source view (5 lines) | View on GitHub

public function first()
{
    $items = $this->getArrayCopy();
    return count($items) ? $items[0] : false;
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback