Array functions


Array PHP function

1) array_change_key_case

  Syntax :  array_change_key_case ( array $array [, int $case = CASE_LOWER ] )
   Returns an array with its keys lower or uppercased, or FALSE if array is not an array.


Example #1 array_change_key_case() example

<?php
$input_array = array("FirSt" => 1, "SecOnd" => 4);
print_r(array_change_key_case($input_array, CASE_UPPER));
?>

The above example will output:
Array
(
    [FIRST] => 1
    [SECOND] => 4
)



2)  array_chunk

  Syntax : array_chunk ( array $array , int $size [, bool $preserve_keys = false ] )
  description : — Split an array into chunks

Example #1 array_chunk() example
<?php
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
?>

The above example will output:
Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => c
            [1] => d
        )

    [2] => Array
        (
            [0] => e
        )

)
Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [2] => c
            [3] => d
        )

    [2] => Array
        (
            [4] => e
        )

)


3).  array_column ()
syntax :      array_column ( array $input , mixed $column_key [, mixed $index_key = null ] )
description :— Return the values from a single column in the input array

Example #1 Get the column of first names from a recordset
<?php
// Array representing a possible record set returned from a database
$records = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    ),
    array(
        'id' => 5342,
        'first_name' => 'Jane',
        'last_name' => 'Jones',
    ),
    array(
        'id' => 5623,
        'first_name' => 'Peter',
        'last_name' => 'Doe',
    )
);

$first_names = array_column($records, 'first_name');
print_r($first_names);
?>
The above example will output:
Array
(
    [0] => John
    [1] => Sally
    [2] => Jane
    [3] => Peter
)


4).  array_combine
description : Creates an array by using one array for keys and another for its values
syntax: array_combine ( array $keys , array $values )

Example #1 A simple array_combine() example
<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);
?>
The above example will output:
Array
(
    [green]  => avocado
    [red]    => apple
    [yellow] => banana
)


5).array_count_values

description :  Counts all the values of an array
syntax : array_count_values ( array $array )
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
The above example will output:
Array
(
    [1] => 2
    [hello] => 2
    [world] => 1
)


6).  array_diff_assoc

description : Computes the difference of arrays with additional index check
syntax: array_diff_assoc ( array $array1 , array $array2 [, array $... ] )

<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>
The above example will output:
Array
(
    [b] => brown
    [c] => blue
    [0] => red
)


7). array_diff_key
description : Computes the difference of arrays using keys for comparison
syntax : array_diff_key ( array $array1 , array $array2 [, array $... ] )

<?php
$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);

var_dump(array_diff_key($array1, $array2));
?>
The above example will output:
array(2) {
  ["red"]=>
  int(2)
  ["purple"]=>
  int(4)
}


8).array_fill_keys
description :  Fill an array with values, specifying keys
syntax : array_fill_keys ( array $keys , mixed $value )

<?php
$keys = array('foo', 5, 10, 'bar');
$a = array_fill_keys($keys, 'banana');
print_r($a);
?>
The above example will output:
Array
(
    [foo] => banana
    [5] => banana
    [10] => banana
    [bar] => banana
)


9).array_diff

description :Computes the difference of arrays
syntax : array_diff ( array $array1 , array $array2 [, array $... ] )

<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>
Multiple occurrences in $array1 are all treated the same way. This will output :
Array
(
    [1] => blue
)


10). array_fill
description : Fill an array with values
syntax   : array_fill ( int $start_index , int $num , mixed $value )

Example #1 array_fill() example
<?php
$a = array_fill(5, 6, 'banana');
$b = array_fill(-2, 4, 'pear');
print_r($a);
print_r($b);
?>
The above example will output:
Array
(
    [5]  => banana
    [6]  => banana
    [7]  => banana
    [8]  => banana
    [9]  => banana
    [10] => banana
)
Array
(
    [-2] => pear
    [0] => pear
    [1] => pear
    [2] => pear
)

11).array_flip

syntax : array_flip ( array $array )
description : Exchanges all keys with their associated values in an array
<?php
$input = array("oranges", "apples", "pears");
$flipped = array_flip($input);

print_r($flipped);
?>
The above example will output:
Array
(
    [oranges] => 0
    [apples] => 1
    [pears] => 2
)


12).array_key_exists
syntax : array_key_exists ( mixed $key , array $array )
description : Checks if the given key or index exists in the array

<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
    echo "The 'first' element is in the array";
}
?>






No comments:

Post a Comment

Pages