Course Banner


PHP Array functions

PHP provides several array functions which is helpful in manipulating array. There are large list of array functions, but below I am providing most used functions list.

Useful Array Function

● array_push()

The array_push() function inserts one or more elements to the end of an array.

Example:

< ?php
  1. $a= array("red","green");
  2. array_push($a, "blue", "yellow");
  3. print"< pre/>";
  4. print_r($a);
  5. ?>

Result:

● array_chunk

array_chunk — Split an array into chunks

Example:

< ?php
  1. $input_array = array('a', 'b', 'c', 'd', 'e');
  2. $chunk = array_chunk($input_array, 4);
  3. print"< pre/>";
  4. print_r($chunk);
  5. ?>

Result:

● array_flip

array_flip — Exchanges all keys with their associated values in an array. In another word, key becomes value and value becomes key.

Example:

< ?php
  1. $trans = array("a" =>1, "b" =>2, "c" =>3);
  2. $trans = array_flip($trans);<
  3. print"< pre>";
  4. print_r($trans);
  5. ?>

Result:

● array_pop

array_pop — Pop the element off the end of array

Example:

< ?php
  1. $arr = array("orange", "banana", "apple", "raspberry");
  2. $fruit = array_pop($arr);
  3. echo"< pre/>";
  4. print_r($arr);
  5. ?>

Result:

● asort

asort — Sort an array and maintain index association. In another word, the array is arranged in ascending order as per its value and not key.

Example:

< ?php
  1. $fruits = array("d" =>"lemon", "a" =>"orange", "b" =>"banana", "c" =>"apple");
  2. asort($fruits);
  3. foreach ($fruits as $key => $val) {
  4. echo"$key = $val"."< br>";
  5. }
  6. ?>

Result:

● current

current — Return the current element in an array

Example:

● end

end — Set the internal pointer of an array to its last element

Example:

< ?php
  1. $fruits = array('apple', 'banana', 'cranberry');
  2. echoend($fruits); // cranberry
  3. ?>


● sizeof

sizeof — Alias of count()

● sort

sort — Sort an array. Sorts an array as per its key and not value.

Example:

< ?php
  1. $fruits = array("lemon", "orange", "banana", "apple");
  2. sort($fruits);
  3. foreach ($fruits as $key => $val) {
  4. echo"fruits[" . $key . "] = ". $val . "\n";
  5. }
  6. ?>

    Result :



● Shuffle

shuffle — Shuffle an array

Example:

< ?php
  1. $numbers = range(1, 20);
  2. shuffle($numbers);
  3. foreach ($numbers as $number) {
  4. echo"$number ";
  5. }
  6. ?>

Result :

It’s like generating random numbers

● ksort

ksort — Sort an array by key

● in_array

in_array — Checks if a value exists in given array or not. It returns Boolean value.

Example:

< ?php
  1. $languages = array("Java", "Angular", "PHP", "Javascript");
  2. if (in_array("Java", $os)) {
  3. echo "Got Irix";
  4. }
  5. if (in_array("Java", $languages)) {
  6. echo "You selected Java";
  7. }
  8. ?>

Result :

● array_diff

Finds difference between two arrays and shows which matches in both the arrays.

Example:

< ?php
  1. $myarray1 = array(1,2,3,4,-1,-2);
  2. $myarray2 = array(-1,-2);
  3. $filtered_array = array_d<iff($myarray1, $myarray2);
  4. print"< pre/>";
  5. print_r($filtered_array);
  6. ?>

Result:

● array_intersect

This array functions shows intersection of two arrays.

Example:

< ?php
  1. $myarray1 = array(1,2,3,4,-1,-2);
  2. $myarray2 = array(-1,-2,3);
  3. $common_array = array_intersect($myarray1,$myarray2);
  4. print"< pre>";
  5. print_r($common_array);
  6. ?>

Result:

● array_unique

This array function is like MySQL unique keyword. Using this function, you can eliminate duplicate array value.

Example:

< ?php
  1. $my_array_with_duplicates= array(1,1,2,2,4,4);
  2. $unique_array = array_unique($my_array_with_duplicates);
  3. print"< pre/>";
  4. print_r($unique_array);
  5. ?>

Result: