Skip to content
Mariusz Łączak edited this page Sep 18, 2013 · 5 revisions

Example usage

callback( string $str )
<?php
use Baseapp\Library\Arr;

// Get the callback function and parameters
list($func, $params) = Arr::callback('Foo::bar(apple,orange)');
 
// Get the result of the callback
$result = call_user_func_array($func, $params);
extract( array $array , array $paths [, mixed $default = NULL ] )
<?php
// Get the values "username", "password" from $_POST
$auth = Arr::extract($_POST, array('username', 'password'));
 
// Get the value "level1.level2a" from $data
$data = array('level1' => array('level2a' => 'value 1', 'level2b' => 'value 2'));
Arr::extract($data, array('level1.level2a', 'password'));
flatten( array $array )
<?php

$array = array('set' => array('one' => 'something'), 'two' => 'other');
 
// Flatten the array
$array = Arr::flatten($array);
 
// The array will now be
array('one' => 'something', 'two' => 'other');
get( array $array , string $key [, mixed $default = NULL ] )
<?php
// Get the value "username" from $_POST, if it exists
$username = Arr::get($_POST, 'username');
 
// Get the value "sorting" from $_GET, if it exists
$sorting = Arr::get($_GET, 'sorting');
is_array( mixed $value )
<?php
// Returns TRUE
Arr::is_array(array());
Arr::is_array(new ArrayObject);
 
// Returns FALSE
Arr::is_array(FALSE);
Arr::is_array('not an array!');
is_assoc( array $array )
<?php
// Returns TRUE
Arr::is_assoc(array('username' => 'john.doe'));
 
// Returns FALSE
Arr::is_assoc('foo', 'bar');
map( mixed $callbacks , array $array [, array $keys = NULL ] )
<?php
// Apply "strip_tags" to every element in the array
$array = Arr::map('strip_tags', $array);
 
// Apply $this->filter to every element in the array
$array = Arr::map(array(array($this,'filter')), $array);
 
// Apply strip_tags and $this->filter to every element
$array = Arr::map(array('strip_tags',array($this,'filter')), $array);
merge( array $array1 , array $array2 )
<?php
$john = array('name' => 'john', 'children' => array('fred', 'paul', 'sally', 'jane'));
$mary = array('name' => 'mary', 'children' => array('jane'));
 
// John and Mary are married, merge them together
$john = Arr::merge($john, $mary);
 
// The output of $john will now be:
array('name' => 'mary', 'children' => array('fred', 'paul', 'sally', 'jane'))
overwrite( array $array1 , array $array2 )
<?php
$a1 = array('name' => 'john', 'mood' => 'happy', 'food' => 'bacon');
$a2 = array('name' => 'jack', 'food' => 'tacos', 'drink' => 'beer');
 
// Overwrite the values of $a1 with $a2
$array = Arr::overwrite($a1, $a2);
 
// The output of $array will now be:
array('name' => 'jack', 'mood' => 'happy', 'food' => 'tacos')
path( array $array , mixed $path [, mixed $default = NULL , string $delimiter = NULL ] )
<?php
// Get the value of $array['foo']['bar']
$value = Arr::path($array, 'foo.bar');


// Get the values of "color" in theme
$colors = Arr::path($array, 'theme.*.color');
 
// Using an array of keys
$colors = Arr::path($array, array('theme', '*', 'color'));
pluck( array $array , string $key )
<?php
// Get all of the "id" values from a result
$ids = Arr::pluck($result, 'id');
range( [ integer $step = integer 10 , integer $max = integer 100 ] )
<?php
// Fill an array with values 5, 10, 15, 20
$values = Arr::range(5, 20);
set_path( array & $array , string $path , mixed $value [, string $delimiter = NULL ] )
<?php
// Set a value on an array by path.
unshift( array & $array , string $key , mixed $val ) (defined in Kohana_Arr)
<?php
// Add an empty value to the start of a select list
Arr::unshift($array, 'none', 'Select a value');