Array functions (1)

Array functions list:

1.- array_​change_​key_​case,
2.- array_​chunk,
3.- array_​column,
4.- array_​combine,
5.- array_​count_​values,
6.- array_​diff_​assoc,
7.- array_​diff_​key
8.- array_​diff_​uassoc,
9.- array_​diff_​ukey,
10.- array_​diff,
11.- array_​fill_​keys,
12.- array_​fill,
13.- array_​filter,
14.- array_​flip,
15.- array_​intersect_​assoc,
16.- array_​intersect_​key,
17.- array_​intersect_​uassoc,
18.- array_​intersect_​ukey,array_​intersect

1.- array_​change_​key_​case

Example #1 array_change_key_case() example

$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
)

Example #2 array_change_key_case() example

Array
(
    [FIRST] => 1
    [SECOND] => 4
)

$input_array = array("FirSt" => 1, "SecOnd" => 4);
print_r(array_change_key_case($input_array, CASE_LOWER));

The above example will output:


Array
(
    [first] => 1
    [second] => 4
)

2.- array_chunk
Example #3 array_chunk() example

$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
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
Example #4 array_column — Return the values from a single column in the input array

// 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
)

Example #5 Get the column of last names from a recordset, indexed by the “id” column

// Using the $records array from Example #1
$last_names = array_column($records, 'last_name', 'id');
print_r($last_names);
Array
(
    [2135] => Doe
    [3245] => Smith
    [5342] => Jones
    [5623] => Doe
)

Example #6 Get the column of usernames from the public “username” property of an object

class User
{
    public $username;

    public function __construct(string $username)
    {
        $this->username = $username;
    }
}

$users = [
    new User('user 1'),
    new User('user 2'),
    new User('user 3'),
];
print_r(array_column($users, 'username'));

The above example will output:

Array
(
    [0] => user 1
    [1] => user 2
    [2] => user 3
)

4.- array_combine
Example #7 array_combine — Creates an array by using one array for keys and another for its values

$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
)

If two keys are the same, the second one prevails.

Example:

print_r(array_combine(Array('a','a','b'), Array(1,2,3)));
Returns:
Array
(
    [a] => 2
    [b] => 3
)

5.- array_count_values
Example #8 array_count_values() example

$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
Example #9 array_diff_assoc() example

$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
print_r($result);
Array
(
    [b] => brown
     => blue
    [0] => red
)

Two values from key => value pairs are considered equal only if (string) $elem1 === (string) $elem2 . In other words a strict check takes place so the string representations must be the same.

$array1 = array(0, 1, 2);
$array2 = array("00", "01", "2");
$result = array_diff_assoc($array1, $array2);
print_r($result);

7.- array_diff_key
Example #10 array_diff_key — Computes the difference of arrays using keys for comparison

$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));

8.- array_diff_uassoc
Example #11 array_diff_uassoc() example – Returns an array containing all the entries from array1 that are not present in any of the other arrays.

The “a” => “green” pair is present in both arrays and thus it is not in the output from the function. Unlike this, the pair 0 => “red” is in the output because in the second argument “red” has key which is 1.

function key_compare_func($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b)? 1:-1;
}

$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_uassoc($array1, $array2, "key_compare_func");
print_r($result);

9.- array_​diff_​ukey

Compare the keys of two arrays (using a user-defined key comparison function), and return the differences:

function myfunction($a,$b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"blue","b"=>"black","e"=>"blue");
$result=array_diff_ukey($a1,$a2,"myfunction");
print_r($result);
// Resut will be Array (  => blue )


Other example
Compare the keys of three arrays (use a user-defined function to compare the keys), and return the differences:


function myfunction($a,$b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}

$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"black","b"=>"yellow","d"=>"brown");
$a3=array("e"=>"purple","f"=>"white","a"=>"gold");

$result=array_diff_ukey($a1,$a2,$a3,"myfunction");
print_r($result);
// Resut will be Array (  => blue )

10.- array_​diff

Compare the values of two arrays, and return the differences:

$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_diff($a1,$a2);
print_r($result);
// Result will be "d"=>"yellow"

11.- array_​fill_​keys

We give keys them and put the values, example

$keys = array('foo', 5, 10, 'bar');
$a = array_fill_keys($keys, 'hola');
print_r($a);
Result:
Array
(
    [foo] => hola
    [5]   => hola
    [10]  => hola
    [bar] => hola
)

Leave a Reply

Your email address will not be published. Required fields are marked *