Kohana 2.3.4
  • Contents
Spis treści
  • Array Helper
    • Methods
      • rotate()
      • remove()
      • extract()
      • binary_search()
      • range()
      • merge()
      • overwrite()
      • map_recursive()
      • unshift_assoc()
      • to_object()
StatusDraft
TodoFill ending methods - verify unshift_assoc example - review to_object method

Array Helper

The Array helper assists in transforming arrays. Warning, in order to use it, class name is 'arr' instead of 'array'.

Methods

rotate()

'rotate' rotates an array (two-dimensional) matrix clockwise. Example, turns a 2×3 array into a 3×2 array.

The two arguments are:

  • [array] The array you want to rotate
  • [boolean] Do you want to keep the same keys in the rotated array? – TRUE by default

Example:

// Please note that the print() statements are for display only
$optical_discs = array
			(
				'CD'  => array('700', '780'),
				'DVD' => array('4700','650'),
				'BD' => array('25000','405')
			);
print Kohana::debug($optical_discs);
$optical_discs = arr::rotate($optical_discs, FALSE);
print ('<br /><br />');
print Kohana::debug($optical_discs);

It will result in HTML as:

Array
(
    [CD] => Array
        (
            [0] => 700
            [1] => 780
        )
 
    [DVD] => Array
        (
            [0] => 4700
            [1] => 650
        )
 
    [BD] => Array
        (
            [0] => 25000
            [1] => 405
        )
 
)
 
 
Array
(
    [0] => Array
        (
            [CD] => 700
            [DVD] => 4700
            [BD] => 25000
        )
 
    [1] => Array
        (
            [CD] => 780
            [DVD] => 650
            [BD] => 405
        )
 
)

remove()

'remove' removes a key from an array and returns it.

The two arguments are:

  • [string] The key you want removed from an array
  • [array] The array you want the key to be removed from

Example:

// Please note that the print() statements are for display only
$optical_discs = array
			(
				'CD'  => array('700', '780'),
				'DVD' => array('4700','650'),
				'BD' => array('25000','405')
			);
print Kohana::debug($optical_discs);
$cd = arr::remove('CD', $optical_discs);
print ('<br />');
print Kohana::debug($cd);
print ('<br />');
print Kohana::debug($optical_discs);

It will result in HTML as:

Array
(
    [CD] => Array
        (
            [0] => 700
            [1] => 780
        )
 
    [DVD] => Array
        (
            [0] => 4700
            [1] => 650
        )
 
    [BD] => Array
        (
            [0] => 25000
            [1] => 405
        )
 
)
 
 
Array
(
    [0] => 700
    [1] => 780
)
 
 
Array
(
    [DVD] => Array
        (
            [0] => 4700
            [1] => 650
        )
 
    [BD] => Array
        (
            [0] => 25000
            [1] => 405
        )
 
)

extract()

'extract' extract ones or more keys from an array. Keys that do not exist in the search array will be NULL in the extracted data.

The two arguments are:

  • [array] The array you want the key to be extracted from
  • [string] The key you want extracted from an array

Example:

 $optical_discs = array
			(
				'CD'  => array('700', '780'),
				'DVD' => array('4700','650'),
				'BD' => array('25000','405')
			);
$optical_discs = arr::extract($optical_discs, 'DVD', 'Bluray');
echo Kohana::debug($optical_discs);

Output:

(array) Array
(
    [DVD] => Array
        (
            [0] => 4700
            [1] => 650
        )
 
    [Bluray] =>  //NULL
)

binary_search()

'binary_search' performs a basic binary search on an array. By default, it returns the key of the array value it finds. The four arguments are:

  • [mixed] The value you want to find.
  • [array] The sorted array you want to search in
  • [boolean] Return the nearest value, or simply return FALSE (the default)
  • [boolean] Sort the array before searching

Example:

$my_array = array('10', '20', '30', '50', '80');
echo arr::binary_search('50', $my_array);
// 3
 
echo arr::binary_search('45', $my_array);
// FALSE (not found)
 
echo arr::binary_search('45', $my_array, TRUE);
// 3
 
echo arr::binary_search('35', $my_array, TRUE);
// 2

range()

'range' fills an array with a range of numbers.

The two arguments are:

  • [integer] Stepping
  • [integer] Ending Number

Example:

echo Kohana::debug(arr::range(17, 150));

Output:

(array) Array
(
    [17] => 17
    [34] => 34
    [51] => 51
    [68] => 68
    [85] => 85
    [102] => 102
    [119] => 119
    [136] => 136
)

merge()

Emulates array_merge_recursive, but appends numeric keys and replaces associative keys, instead of appending all keys. It takes:

  • [array] any number of arrays
  • returns [array] the merged array

Example:

echo Kohana::debug(arr::merge(array('a', 'b'), array('c', 'd'), array('e' => array('f', 'g'))));

It will result as:

(array) Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [e] => Array
        (
            [0] => f
            [1] => g
        )
 
)

overwrite()

'overwrite' overwrites an array with values from input array(s). Note that non-existing keys will not be appended. It takes:

  • [array] key array
  • [array] input array(s) that will overwrite key array values

Example:

$array1 = array('fruit1' => 'apple', 'fruit2' => 'mango', 'fruit3' => 'pineapple');
$array2 = array('fruit1' => 'strawberry', 'fruit4' => 'coconut');
print Kohana::debug(arr::overwrite($array1, $array2));

Output:

(array) Array
(
    [fruit1] => strawberry
    [fruit2] => mango
    [fruit3] => pineapple
)

map_recursive()

map_recursive($callback, array $array) has been created because PHP does not have this function, and array_walk_recursive creates references in arrays and is not truly recursive. It takes:

  • [array] $callback a valid callback to apply to each member of the array
  • [array] $array array to map to
  • returns [array] the mapped array

Example :

public function add($value){
    return $value + 1;
}
 
echo Kohana::debug(arr::map_recursive(array($this, 'add'), array('a' => 1, 'b' => 2, 'c' => array(3, 4), 'd' => array('e' => 5))));

It will result as:

(array) Array
(
    [a] => 2
    [b] => 3
    [c] => Array
        (
            [0] => 4
            [1] => 5
        )
 
    [d] => Array
        (
            [e] => 6
        )
)

unshift_assoc()

unshift_assoc has been created because PHP does not have this function. It just unshift an association in an associative array. It takes:

  • [array] array to unshift
  • [string] key to unshift
  • [mixed] value to unshift

Example

$fruits = array('fruit1' => 'apple', 'fruit2' => 'mango', 'fruit3' => 'pineapple');
arr::unshift_assoc($fruits, 'fruit1', 'strawberry');
print Kohana::debug($fruits);

Output

(array) Array
(
    [fruit1] => strawberry
    [fruit2] => mango
    [fruit3] => pineapple
)

to_object()

to_object(array $array, $class = 'stdClass') converts an array to an object. This method supports multi level arrays. It takes:

  • [array] $array array to convert
  • [string] $class the base class (default 'stdClass')

Note: For 1-level arrays, use Typecasting $object = (object) $array;

Example

$array = arr::to_object(array('test' => 13));
print $array ->test;
print Kohana::debug($array);

Output

13
(object) stdClass Object
(
    [test] => 13
)
helpers/arr.txt · ostatnio zmienione: 2009-02-10 22:31 przez michael
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki