PageArray/WireArray


Getting Items

$a->implode([$delim], $key)

Introduced in Version 2.4

Description

Implode all elements to a delimiter-separated string containing the given key/property from each item. Similar to PHP's implode() function.

Usage may include:

$string = $a->implode($delimiter, $property);
$string = $a->implode($delimiter, $property, $options); 
$string = $a->implode($property);
$string = $a->implode($property, $options); 
$string = $a->implode(function($item, $key) { ... }); 

$delimiter (string, optional)
The delimiter to separate each item by (or the glue to tie them together). If not needed, this argument may be omitted and $property supplied first (also shifting $options to 2nd argument).

$property (string, required)
The property to retrieve from each item (i.e. "title"), or a function that returns the value to store. If a function/closure is provided it is given the $item (argument 1) and the $key (argument 2), and it should return the value (string) to use.

$options (array, optional)
When used, an array with modifiers to the behavior: 
skipEmpty: Whether empty items should be skipped (default=true)
prepend: String to prepend to result. Ignored if result is blank.
append: String to prepend to result. Ignored if result is blank.

$items = $pages->find("template=basic-page"); 

// render all the titles, each separated by a <br>, for each page in $items
echo $items->implode('<br>', 'title'); 

// render an unordered list of each item's title
echo "<ul><li>";
echo $items->implode('</li><li>', 'title');
echo "</li></ul>";

// same as above, but using prepend/append options, 
// this ensures no list generated when $items is empty
echo $items->implode('</li><li>', 'title', array(
  'prepend' => '<ul><li>', 
  'append' => '</li></ul>'
)); 

// same as above, but with all items now presented as links
// this demonstrates use of $property as a function. note that
// we are also omitting the delimiter here as well, since we don't need it
echo $items->implode(function($item) {
  return "<li><a href='$item->url'>$item->title</a></li>";
}, array('prepend' => '<ul>', 'append' => '</ul>'));  


Post Comment