X

Rate Us :



Share with Friends :

Menu

PHP-Array :

Array is a collection of heterogeneous (dissimilar) data types. PHP is loosely typed language that's why we can store any type of values in Array.

Normal variable can store single value but Array can store multiple values.

Array contains number of elements, each element is a combination of element key & element value.

The key of first element is '0' and last element is 'total elements - 1'.

Syntax of Array Declaration :

variable_name = array(element1, element2, element3......);

Example 1 :

<?php $arr = array(10, 20, 30); print_r($arr); ?>

Output :

Array ( [0] => 10 [1] => 20 [2] => 30 )
// print_r is used to display the entire Array

Example 2 :

<?php $arr = array(10, 20, 30); echo $arr[1]; ?>

Output :

20
// echo can be used to display index wise element

Example 3 :

// We can create Array with an explicit keys.

<?php $arr = array(0 => 10, 1 => 20, 2 => 30); print_r($arr); ?>

Output :

Array ( [0] => 10 [1] => 20 [2] => 30 )

Note :

If we don't provide an explicit key to an Array element, the new key is provided by default depending upon previous keys of that element.


Example 4 :

<?php $arr = array(10, 100 => 20, 200 => 30); print_r($arr); ?>

Output :

Array ( [0] => 10 [100] => 20 [200] => 30 )

Example 5 :

<?php $arr = array(10, 20, 0 => 30); print_r($arr); ?>

Output :

Array ( [0] => 30 [1] => 20 ) // Here, the Array element key '0' is overwritten by new value as "30"

Example 6 :

<?php $arr = array(); $arr[0] = 10; $arr[1] = 20; print_r($arr); ?>

Output :

Array ( [0] => 10 [1] => 20 ) // Here, we have declared an Array named as $arr.

Example 7 :

<?php $arr = array(); $arr['Language'] = "PHP"; $arr['Manager'] = "Man"; print_r($arr); ?>

Output :

Array ( [Language] => PHP [Manager] => Man ) // Here, we have declared an Array named as $arr and as been shown we can provide String as Array element key. This concept is called as 'Associative Array'

Functions used in Array :

count() :

This function returns total number of Array elements.

Example :

<?php $arr = array(10, 20, 30); echo count($arr); ?>

Output :

3 // Here, we have declared an Array named as $arr

sort() :

This function arranges the Array elements in ascending order with new keys.

Example 1 :

<?php $arr = array(10, 20, 3, 30, 15); sort($arr); print_r($arr); ?>

Output :

Array ( [0] => 3 [1] => 10 [2] => 15 [3] => 20 [4] => 30 )

Example 2 :

<?php $arr = array(10, 'a' => 20, 3, 30, 15); sort($arr); print_r($arr); ?>

Output :

Array ( [0] => 3 [1] => 10 [2] => 15 [3] => 20 [4] => 30 )

rsort() :

This function arranges the Array elements in descending order with new keys.

Example 1 :

<?php $arr = array(10, 20, 3, 30, 15); rsort($arr); print_r($arr); ?>

Output :

Array ( [0] => 30 [1] => 20 [2] => 15 [3] => 10 [4] => 3 )

Example 2 :

<?php $arr = array(10, 'a' => 20, 3, 30, 15); rsort($arr); print_r($arr); ?>

Output :

Array ( [0] => 30 [1] => 20 [2] => 15 [3] => 10 [4] => 3 )

asort() :

This function arranges the Array elements in ascending order with original keys.

Example :

<?php $arr = array(10, 'a' => 20, 3, 30, 15); asort($arr); print_r($arr); ?>

Output :

Array ( [1] => 3 [0] => 10 [3] => 15 [a] => 20 [2] => 30 )

arsort() :

This function arranges the Array elements in descending order with original keys.

Example :

<?php $arr = array(10, 'a' => 20, 3, 30, 15); arsort($arr); print_r($arr); ?>

Output :

Array ( [2] => 30 [a] => 20 [3] => 15 [0] => 10 [1] => 3 )

ksort() :

This function arranges the Array elements in ascending order with element keys.

Example :

<?php $arr = array(10, 20, 3, 30, 15); ksort($arr); print_r($arr); ?>

Output :

Array ( [0] => 10 [1] => 20 [2] => 3 [3] => 30 [4] => 15 )

krsort() :

This function arranges the Array elements in descending order with element keys.

Example :

<?php $arr = array(10, 20, 3, 30, 15); krsort($arr); print_r($arr); ?>

Output :

Array ( [4] => 15 [3] => 30 [2] => 3 [1] => 20 [0] => 10 )

array_sum() :

This function is used to get the sum of Array elements.

Example 1 :

<?php $arr = array(10, 20, 30); echo array_sum($arr); ?>

Output :

60

Example 2 :

<?php $arr = array(10, 20, 30, 'Man'); echo array_sum($arr); ?>

Output :

60

array_product() :

This function is used to get the product of Array elements.

Example :

<?php $arr = array(10, 20, 30); echo array_product($arr); ?>

Output :

6000

array_push() :

By using this function, we can add new elements at the end point of Array & it returns total number of Array elements.

Example 1 :

<?php $arr = array(10, 20, 30); echo array_push($arr, 40); ?>

Output :

4

Example 2 :

<?php $arr = array(10, 20, 30); array_push($arr, 40); print_r($arr); ?>

Output :

Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 )

array_pop() :

By using this function, we can delete the last element of an Array & it returns the value of that element which is being deleted.

Example 1 :

<?php $arr = array(10, 20, 30); echo array_pop($arr); ?>

Output :

30

Example 2 :

<?php $arr = array(10, 20, 30); array_pop($arr); print_r($arr); ?>

Output :

Array ( [0] => 10 [1] => 20 )

array_shift() :

This function removes the first element of an Array & returns the value of that element.

Example :

<?php $arr = array(10, 20, 30); echo array_shift($arr); ?>

Output :

10

array_unshift() :

By using this function, we can add an element at the beginning of an Array & returns the total number of Array elements.

Example :

<?php $arr = array(10, 20, 30); echo array_unshift($arr, 5); ?>

Output :

4

array_combine() :

This function creates a new array by combining the elements of two Arrays. New Array keys are values of first Array. New array values are values of second Array.

Example :

<?php $arr1 = array(10, 20, 30); $arr2 = array('a', 'b', 'c'); $arr = array_combine($arr1, $arr2); print_r($arr); ?>

Output :

Array ( [10] => a [20] => b [30] => c )

array_diff() :

This function compares the values of two Array elements & returns the differences from first Array.

Example :

<?php $arr1 = array(10, 20, 30); $arr2 = array(10, 30, 40); $arr = array_diff($arr1, $arr2); print_r($arr); ?>

Output :

Array ( [1] => 20 )

array_diff_assoc() :

This function compares the keys and values of two Arrays & returns the differences from first Array.

Example :

<?php $arr1 = array(10, 20, 30); $arr2 = array(10, 30, 40); $arr = array_diff_assoc($arr1, $arr2); print_r($arr); ?>

Output :

Array ( [1] => 20 [2] => 30 )

array_diff_key() :

This function compares the two Array keys & returns the differences from first Array.

Example :

<?php $arr1 = array(10, 20, 30); $arr2 = array(10, 2 => 30, 40); $arr = array_diff_key($arr1, $arr2); print_r($arr); ?>

Output :

Array ( [1] => 20 )

explode() :

This function divides a String as Array elements based on input value.

Example :

<?php $str = "Welcome"; $arr = explode("l",$str); print_r($arr); ?>

Output :

Array ( [0] => We [1] => come )

implode() :

This function combines Array elements as a String.

Example :

<?php $arr = array('A', 'B', 'C'); echo implode("",$arr); ?>

Output :

ABC

extract() :

This function divides the Associative Array elements as variables from Array.

Example :

<?php $arr = array('a' => 10, 'b' => 20); extract($arr); echo $a."\n"; echo $b; ?>

Output :

10 20

list() :

By using this function, we can put Array elements into the variables.

Example :

<?php list($a, $b) = array(10, 20); echo $a; echo $b; ?>

Output :

1020

in_array() :

By using this function, we can check whether the input value is existed in the specified Array or not. If the value is existed it returns true (1), otherwise it returns false (0).

Example :

<?php $arr = array(10, 20, 30); echo in_array(20, $arr); ?>

Output :

1

array_search() :

This function searches that the input value is existed in specified Array or not and returns key of that element.

Example :

<?php $arr = array(10, 20, 30); echo array_search(30, $arr); ?>

Output :

2

array_rand() :

By using this function, we can get the keys of an Array randomly.

Example :

<?php $arr = array('b' => 10, 'a' => 20, 123 => 30); print_r(array_rand($arr, 2)); ?>

Output :

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

array_slice() :

By using this function, we can get some parts of an Array.

Example :

<?php $arr = array(10, 20, 30, 40); print_r(array_slice($arr, 0, 3)); ?>

Output :

Array ( [0] => 10 [1] => 20 [2] => 30 )

array_count_values() :

This function returns an Array with number of occurrences for each value.

Example :

<?php $arr = array(10, 20, 30, 40); print_r(array_count_values($arr)); ?>

Output :

Array ( [10] => 1 [20] => 1 [30] => 1 [40] => 1 )

array_key_exists() :

By using this function, we can check whether the input key is existed in the specified Array or not.

Example :

<?php $arr = array(10, 'a' => 20, 30, 40); echo array_key_exists('a', $arr); ?>

Output :

1

array_keys() :

This function returns all the keys of an Array as a new Array.

Example :

<?php $arr = array('x' => 10, 'a' => 20, 'b' => 40); print_r(array_keys($arr)); ?>

Output :

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

array_merge() :

This function is used to merge the elements of two Arrays.

Example :

<?php $arr1 = array(10, 20, 30); $arr2 = array(40, 50); $arr = array_merge($arr1, $arr2); print_r($arr); ?>

Output :

Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 50 )

array_reverse() :

This function displays Array elements in the reverse direction.

Example :

<?php $arr = array(10, 20, 30); print_r(array_reverse($arr)); ?>

Output :

Array ( [0] => 30 [1] => 20 [2] => 10 )

array_values() :

This function returns all the values of an Array as a new Array.

Example :

<?php $arr = array(10, 'a' => 20, 30); print_r(array_values($arr)); ?>

Output :

Array ( [0] => 10 [1] => 20 [2] => 30 )

shuffle() :

This function shuffles the elements of an Array.

Example :

<?php $arr = array(10, 'a' => 20, 30); shuffle($arr); print_r($arr); ?>

Output :

Array ( [0] => 20 [1] => 10 [2] => 30 )
This function gives random output.