map ( $callback, $bind )

Passes each element in the current CFArray object through a function, and produces a new CFArray object containing the return values.

The callback function takes three parameters:

  • $item - mixed - Optional - The individual node in the array.
  • $key - mixed - Optional - The key for the array node.
  • $bind - mixed - Optional - The variable that was passed into the $bind parameter.

Access

public

Parameters

Parameter

Type

Required

Description

$callback

string
function

Required

The callback function to execute. PHP 5.3 or newer can use an anonymous function.

$bind

mixed

Optional

A variable from the calling scope to pass-by-reference into the local scope of the callback function.

Returns

Type

Description

CFArray

A new CFArray object containing the return values.

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 (12 lines) | View on GitHub

public function map($callback, &$bind = null)
{
    $items = $this->getArrayCopy();
    $collect = array();

    foreach ($items as $key => &$item)
    {
        $collect[] = $callback($item, $key, $bind);
    }

    return new CFArray($collect);
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback