Fuel Documentation

Arr Class

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

to_assoc($arr)

The to_assoc method turns a non-associative array into an associative array if it has an even number of segments. If it has an odd number of segments it returns null.

Static Yes
Parameters
Param Default Description
$arr required The array to convert.
Returns array|null
Example
$arr = array('foo', 'bar', 'baz', 'yay');
$array = Arr::to_assoc($arr);
/*
Result:
array(
    'foo' => 'bar',
    'baz' => 'yay',
)
*/

$arr = array('foo', 'bar', 'baz');
$array = Arr::to_assoc($arr);
/*
Result:
null
*/

assoc_to_keyval($assoc = null, $key_field = null, $val_field = null)

The assoc_to_keyval method turns a multi-dimensional array into a key=>val array.

Static Yes
Parameters
Param Default Description
$assoc required The array to transform.
$key_field required The associative array field to map as the key.
$val_field required The associative array field to map as the value.
Returns array
Example
$people = array(
	array(
		"name" => "Jack",
		"age" => 21
	),
	array(
		"name" => "Jill",
		"age" => 23
	)
);

print_r( Arr::assoc_to_keyval($people, 'name', 'age') );

// Returns
Array
(
	["Jack"] => 21
	["Jill"] => 23
)

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($array, $glue = ':', $reset = true)

The flatten method flattens a multi-dimensional array (both associative and indexed) down into a 1 dimensional 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?
Returns array
Example
$indexed = array(
	array(
		"a"
	),
	array(
		"b"
	),
	array(
		"c"
	),
);

print_r( Arr::flatten($indexed, '_') );

// Returns
Array
(
	[0_0] => a
	[0_1] => b
	[0_2] => c
)

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" => "Fuel",
	"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] => Fuel
	[project_type] => Framework
)

filter_keys($array, $keys, $remove = false)

The filter_keys method filters a given array to a set of keys. It returns an array that contains only the items whose keys are in the $keys array. Can also remove the specified $keys from an array.

Static Yes
Parameters
Param Default Description
$array required The array to filter.
$keys required Array of keys to filter the above array with.
$remove
false
If true, removes the $keys from the $array instead of fetching them.
Returns array
Example
$arr = array(
	"user_name" => "John",
	"user_surname" => "Lastname",
	"project_name" => "Fuel",
	"project_type" => "Framework",
);

print_r( Arr::filter_keys($arr, array('project_name', 'user_name')));

// Returns
Array
(
	[project_name] => Fuel
	[user_name] => John
)

// Let's remove some keys
print_r( Arr::filter_keys($arr, array('user_name', 'user_surname', true)));

// Returns
Array
(
	[project_name] => Fuel
	[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_value($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 order (asc or desc).
$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'
			)
		),
	),
);

Procedural helpers

in_arrayi($needle, $haystack)

The in_arrayi function is a case-insensitive version of in_array.

Parameters
Param Type Default Description
$needle string required the value to search for
$haystrack array required the array to search in
Returns bool
Example
$bool = in_arrayi('This', array('something','tHis')); // Will result in $bool == true $bool = in_arrayi('Thi', array('something','tHis')); // Will result in $bool == false