Fuel Documentation

Arr Class

The arr class is a set of helper functions for working with arrays.

average($array = null)

The average method takes all values of an array and returns the average value.

Static Yes
Parameters
Param Default Description
$array Required Array of values to average.
Returns array
Example
echo Arr::average(array('1', 2, 4, '8')); // Outputs 3.75

flatten_assoc($array, $glue = ':', $reset = true)

The flatten_assoc method flattens a multi-dimensional associative array down into a 1 dimensional associative array.

Static Yes
Parameters
Param Default Description
$array Required The array to flatten.
$glue
:
The string used to glue the keys together with
$reset
true
Should we create a new array of values instead of merging them into the last array created by flatten_assoc?
Returns array
Example
$people = array(
	array(
		"name" => "Jack",
		"age" => 21
	),
	array(
		"name" => "Jill",
		"age" => 23
	)
);

print_r( Arr::flatten_assoc($people) );

// Returns
Array
(
	[0:name] => Jack
	[0:age] => 21
	[1:name] => Jill
	[1:age] => 23
)

// Let's flatten another array on top
print_r( Arr::flatten_assoc( array( array( "name" => "Humpty", "age" => 11 ) ) ,":",false) );

// Returns
Array
(
	[0:name] => Humpty
	[0:age] => 11
	[1:name] => Jill
	[1:age] => 23
)

filter_prefixed($array, $prefix = 'prefix_', $remove_prefix = true)

The filter_prefixed method filters the array on a prefix. It returns an array where the key starts with the specified prefix.

Static Yes
Parameters
Param Default Description
$array Required The array to filter.
$prefix
prefix_
The string used to filter on
$remove_prefix
true
Remove or keep the prefix in the array key?
Returns array
Example
$arr = array(
	"user_name" => "John",
	"user_surname" => "Lastname",
	"project_name" => "FuelPHP",
	"project_type" => "Framework",
);

print_r( Arr::filter_prefixed($arr, "user_"));

// Returns
Array
(
	[name] => Frank
	[surname] => de Jonge
)

// Let's keep the prefix
print_r( Arr::filter_prefixed($arr, "project_", false);

// Returns
Array
(
	[project_name] => FuelPHP
	[project_type] => Framework
)

element($array, $key, $default = false)

The element method returns the element of the given array or a default if it is not set.

Static Yes
Parameters
Param Default Description
$array Required The array to access
$key Required The key requested in $array.
$default
false
The value to be returned if the requested key does not exist
Returns mixed
Example
$person = array(
	"name" => "Jack",
	"age" => "21",
	"location" => array(
		"city" => "Pittsburgh",
		"state" => "PA",
		"country" => "US"
	)		 
);

echo Arr::element($person, "name", "Unknown Name");
// Returns "Jack"

echo Arr::element($person, "job", "Unknown job");
// Returns "Unknown job"

// This method can also dive into arrays by using a dot to separate between keys
echo Arr::element($person, "location.city", "Unknown City");
// Returns "Pittsburgh"

insert(Array &$original, $value, $pos)

The insert method is mainly an array_splice alias with added error checking
WARNING: The original array is edited by reference, only boolean success is returned

Static Yes
Parameters
Param Default Description
$original Required The array to use
$value Required The value(s) to insert
$pos Required The numeric position at which to insert, negative to count from the end backwards
Returns boolean
Example
$people = array("Jack", "Jill");

// Add one value
Arr::insert($people, "Humpty", 0 );
print_r( $people );
// Returns 
Array
(
	[0] => Humpty
	[1] => Jack
	[2] => Jill
)

// Add multiple values
Arr::insert($people, array("Hansel", "Gretel"), 1 );
print_r( $people );
// Returns
Array
(
	[0] => Humpty
	[1] => Hansel
	[2] => Gretel
	[3] => Jack
	[4] => Jill
)

// Add an array
Arr::insert($people, array( array("name" => "Wolf", "teeth" => "sharp" ) ), 0 );
print_r( $people );

// Returns
Array
(
	[0] => Array
		(
			[name] => Wolf
			[teeth] => sharp
		)

	[1] => Humpty
	[2] => Hansel
	[3] => Gretel
	[4] => Jack
	[5] => Jill
)

insert_after_key(Array &$original, $value, $key)

The insert_after_key method adds an element to an array after the key specified
WARNING: The original array is edited by reference, only boolean success is returned

Static Yes
Parameters
Param Default Description
$original Required The array to use
$value Required The value(s) to insert
$key Required The key after which to insert
Returns boolean
Example
$people = array("Jack", "Jill");

Arr::insert_after_key($people, "Humpty", 1 );
print_r( $people );

// Returns
Array
(
	[0] => Jack
	[1] => Jill
	[2] => Humpty
)

insert_after_value(Array &$original, $value, $search)

The insert_after_value method adds an element to an array after the value specified
WARNING: The original array is edited by reference, only boolean success is returned

Static Yes
Parameters
Param Default Description
$original Required The array to use
$value Required The value(s) to insert
$search Required The value after which to insert
Returns boolean
Example
$people = array("Jack", "Jill");

Arr::insert_after_key($people, "Humpty", "Jack" );
print_r( $people );

// Returns
Array
(
	[0] => Jack
	[1] => Humpty
	[2] => Jill
)

sort($array, $key, $order = 'asc', $sort_flags = SORT_REGULAR)

The sort method sorts a multi-dimensional array by its values.

Static Yes
Parameters
Param Default Description
$array Required The array to sort
$key Required The key requested to sort by in $array.
$order
'asc'
The value to be returned if the requested key does not exist
$sort_flags
SORT_REGULAR
The php sort type flags (http://php.net/manual/en/function.sort.php)
Returns mixed
Example
$data = array(
	array(
		'info' => array(
			'pet' => array(
				'type' => 'dog'
			)
		),
	),
	array(
		'info' => array(
			'pet' => array(
				'type' => 'fish'
			)
		),
	),
	array(
		'info' => array(
			'pet' => array(
				'type' => 'cat'
			)
		),
	),
);

$data = Arr::sort($data, 'info.pet.type');
// Returns
array(
	array(
		'info' => array(
			'pet' => array(
				'type' => 'cat'
			)
		),
	),
	array(
		'info' => array(
			'pet' => array(
				'type' => 'dog'
			)
		),
	),
	array(
		'info' => array(
			'pet' => array(
				'type' => 'fish'
			)
		),
	),
);