PHP array functions

PHP array functions
5 (100%) 1 vote

List of functions that work with php arrays.

array_shift()

array_shift() – Shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won’t be touched.

$numbers = [1,2,3,4,5,6];
array_shift($numbers);
print_r($numbers);

//Result:
/*Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 6
)*/

array_pop()

array_pop() – pop the last element out of an array

$numbers = [1,2,3,4,5,6];
array_pop($numbers);
print_r($numbers);

//Result:
/*Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)*/

!The difference between array_shift() and array_pop() is that the array_shift() drag the first element from array while array_pop() drag the last element.

array_unshift()

array_unshift() — Add one or more elements to the beginning of an array

$colors = ['red','white','blue'];
array_unshift($colors,'black');
print_r($colors);

//Result:
/*Array
(
    [0] => black
    [1] => red
    [2] => white
    [3] => blue
)*/

array_push()

array_push() – Push one or more elements onto the end of array

$colors = ['red','white','blue'];
array_push($colors,'black');
print_r($colors);

//Result:
/*Array
(
    [0] => red
    [1] => white
    [2] => blue
    [3] => black
)*/

!The difference between array_unshift() and array_push() is that the array_unshift() add the first element from array while array_push() add the last element.

count

count() – returns the number of elements as an integer.

$numbers = [1,2,3,4,5,6];
echo count($numbers);
// result: 6

Attention! For counting all the elements of a multidimensional array, we need to use the optional mode parameter COUNT_RECURSIVE.

$cities = array("France"=>array("Domme","Paris", "Cannes"), "Uk"=>array("London", "Newport", "Manchester"));
echo count($cities);
//Result: 2

echo count($cities,COUNT_RECURSIVE);
//Result: 8

sort

sort() – sorts an array from lowest to highest values. If two values as equal, their relative order in the sorted array is undefined.

SORT_REGULAR – compare items normally (don’t change types)
SORT_NUMERIC – compare items numerically
SORT_STRING – compare items as strings

$number = [7,5,2,1,1,0,6];
sort($number);
print_r($number);

//Result
Array
(
    [0] => 0
    [1] => 1
    [2] => 1
    [3] => 2
    [4] => 5
    [5] => 6
    [6] => 7
)

asort

asort() – Sort an array and maintain index association. Asort() is used mainly when sorting associative arrays where the actual element order is significant.

$numbers = array(8,5,8,1,2,4,0);
asort($numbers);
print_r($numbers);

//Result
Array
(
    [6] => 0
    [3] => 1
    [4] => 2
    [5] => 4
    [1] => 5
    [0] => 8
    [2] => 8
)

array_slice

array_slice() — Extract a slice of the array.

To use it, pass it the array to extract the slice from, followed by the position of the first element (counting from zero), followed by the number of elements to extract.

Attention! The function returns a new array containing copies of the elements you extracted (it doesn’t touch the original array).

$cities = ["London", "Paris", "Roma", "Praga", "Berlin"];
print_r(array_slice($cities,1,3));

//Result
Array
(
    [0] => Paris
    [1] => Roma
    [2] => Praga
)

You can use array_slice() with associative arrays to.

If the third argument to array_slice() is missing the function extracts all elements from the start position to the end of the array.

in_array

in_array() checks if a value exists in an array. Returns TRUE if the element is found in the array, FALSE otherwise.

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

Also, there is a third parameter, default set as false (bool $strict = FALSE). If the third parameter $strict is set to TRUE then in_array() will also check the types of the element.

$numbers = [7,9,1,1,6,5];

//Default
if(in_array("1", $numbers)){
    echo "1. The element is in array";
}

//strict is set to TRUE

if(in_array("1", $numbers, TRUE)){
    echo "2. The element is in array";
}

//Result
1. The element is in array

Leave a Reply

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