-
Notifications
You must be signed in to change notification settings - Fork 19
Mariusz Łączak edited this page Sep 18, 2013
·
5 revisions
<?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);
<?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'));
<?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');
<?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');
<?php
// Returns TRUE
Arr::is_array(array());
Arr::is_array(new ArrayObject);
// Returns FALSE
Arr::is_array(FALSE);
Arr::is_array('not an array!');
<?php
// Returns TRUE
Arr::is_assoc(array('username' => 'john.doe'));
// Returns FALSE
Arr::is_assoc('foo', 'bar');
<?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);
<?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'))
<?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')
<?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'));
<?php
// Get all of the "id" values from a result
$ids = Arr::pluck($result, 'id');
<?php
// Fill an array with values 5, 10, 15, 20
$values = Arr::range(5, 20);
<?php
// Set a value on an array by path.
<?php
// Add an empty value to the start of a select list
Arr::unshift($array, 'none', 'Select a value');