Array Helper
This helper adds functionality not available in the CodeIgniter Array Helper.
Functions
array_object_merge(&$object, $array)
Merge an array or an object into another object. Returns the merged object.
Name | Default | Required | Description |
---|---|---|---|
object | Yes | The Object to merge into. Passed by reference. | |
array | Yes | The Array or Object to merge |
array_for_select(args)
Reformat an array to be ready for the HTML Select helper or other shenanigans.
This function can behave in different ways based on how you pass in your arguments. Please refer to one of the following formats:
array_for_select($array, $keys_key, $values_key)
This is the most common usage and will reformat your array so that the $keys_key will be the new array keys and the $values_key will be the new values. See code example below.
Name | Default | Required | Description |
---|---|---|---|
array | Yes | The Array of data. See format below. | |
keys_key | Yes | The key to the new keys value | |
values_key | Yes | The key to the new values value |
// input array
$array = array(
array('id' => 1, 'title' => 'One'),
array('id' => 2, 'title' => 'Two'),
array('id' => 3, 'title' => 'Three')
);
// usage
$result = array_for_select($array, 'id', 'title');
// $result
Array
(
[1] => One
[2] => Two
[3] => Three
);
array_for_select($array, $values_key)
This is used when you have key / value pairs and want to preserve the keys. This will reformat your array so that the $array's key will be the new array keys and the $values_key will be the new values. See code example below.
Name | Default | Required | Description |
---|---|---|---|
array | Yes | The Array of data. See format below. | |
values_key | Yes | The key to the new values value |
// input array
$array = array(
'item_1' => array('id' => 1, 'title' => 'One'),
'item_2' => array('id' => 2, 'title' => 'Two'),
'item_3' => array('id' => 3, 'title' => 'Three')
);
// usage
$result = array_for_select($array, 'title');
// $result
Array
(
[item_1] => One
[item_2] => Two
[item_3] => Three
);
array_for_select($array)
This is used when you have a collection of values that need the same keys and values. This will reformat your array so that the $array's key will be the new array key and value. See code example below.
Name | Default | Required | Description |
---|---|---|---|
array | Yes | The Array of data. See format below. |
// input array
$array = array(
'One',
'Two',
'Three'
);
// usage
$result = array_for_select($array);
// $result
Array
(
[One] => One
[Two] => Two
[Three] => Three
);
assoc_array_prop(array &$arr = null, $prop = 'id')
Reformat an array so the keys are $prop
's value and preserve each items data.
Name | Default | Required | Description |
---|---|---|---|
array | Yes | The Array of data. Assigned by reference. | |
prop | id | No | The key to the new keys value |
// data
$array = array(
array('id' => 1, 'title' => 'One', 'order' => 2),
array('id' => 3, 'title' => 'Three', 'order' => 1),
array('id' => 4, 'title' => 'Four', 'order' => 3)
);
// reformat
assoc_array_prop($array, 'id');
// result
Array
(
[1] => Array
(
[id] => 1
[title] => One
[order] => 2
)
[3] => Array
(
[id] => 3
[title] => Three
[order] => 1
)
[4] => Array
(
[id] => 4
[title] => Four
[order] => 3
)
)