Top Most Popular PHP Array Functions
There are lot of functions related to array in PHP but we will focus on the most commonly used Functions for manipulating Arrays in PHP.
1. count()
To count the number of elements in an array, use count() function. We can also use sizeof() function to get the number of elements in an array. sizeof() function is an alias of count() function.

Example 1:

 <?php
 
 // Definition of array for PHP version 5.3 or below
 $cars = array("Ferrari", "Mercedes", "BMW", "Benz");  
 
 // Definition of array for PHP version 5.4 or above
 $cars = ["Ferrari", "Mercedes", "BMW", "Benz"];  
 echo count($cars);
				
 ?>

 /*

 Output :

 4

 */

                            

Example 2:

 <?php

// Definition of array for PHP version 5.3 or below 
$cars = array(
	'Ferrari' => array(
		'FI585',
		'FI909'
	) ,
	'Mercedes' => array(
		'M39',
		'M69'
	) ,
	'Benz' => array(
		'BZ01'
	)
);

// Definition of array for PHP version 5.4 or above
$cars = [
        'Ferrari' => [
                'FI585',
                'FI909'
                ],
        'Mercedes' => [
                'M39',
                'M69'
                ],
        'Benz' => [
                'BZ01'
                ]        
        ];

echo "Normal count: " . count($cars)."
"; echo "Recursive count: " . count($cars,1); ?> /* Output : Normal count: 3 Recursive count: 8 */

Example 3:

 <?php

// Definition of array for PHP version 5.4 or above
$user_details = [
    'name' => 'Rahul Yadav',
    'email' => 'admin@codephponline.com',
    'phone' => [
        'home' => '999999XXXX',
        'office' => '888888XXXX',
        ],
   'country' => 'India',
];

echo "Normal count: " . count($user_details)."
"; echo "Recursive count: " . count($user_details,1); ?> /* Output : Normal count: 4 Recursive count: 6 */
2. array_merge()

The array_merge() function is used to combine data from two or more arrays into a single array structure. Any duplicate keys are overwritten by later elements in later arrays. Keys are merged in the order of the arrays that were passed.

Example 1:

 <?php
 
$ax = [
     'a' => 'alpha',
     'b' => 'bravo',
     'c' => 'charlie',
     'd' => 'delta'
     ];
	  
$ay = [
     'p' => 'tango',
	 'q' => 'uniform',
	 'r' => 'victor',
	 's' => 'rosy',
	 't' => 'ray'
	 ];
	 
$az = [
     'u' => 'lima',
     'v' => 'mike',
     'w' => 'beta'
      ];
	
$merged_array = array_merge($ax, $ay, $az);

	  
 echo "<pre>";
 print_r($merged_array);
				
 ?>

 /*

 Output :

 Array
 (
    [a] => alpha
    [b] => bravo
    [c] => charlie
    [d] => delta
    [p] => tango
    [q] => uniform
    [r] => victor
    [s] => rosy
    [t] => ray
    [u] => lima
    [v] => mike
    [w] => beta
 )

 */

                            
3. array_keys()

The array_keys() function retrieves all the keys from an array and returns them as an indexed array.

Example 1:

 <?php
 
$array_1 = [
	   'Orange' => 100,
	   'Apple' => 200,
	   'Banana' => 300,
	   'Cherry' => 400
];
	  
$array_2 = [
            'tango',
            'uniform',
            'victor',
            'rosy',
            'ray'
];
	
 echo "<pre>";
 print_r(array_keys($array_1));
 print_r(array_keys($array_2));
 
				
 ?>

 /*

 Output :

 Array
 (
    [0] => Orange
    [1] => Apple
    [2] => Banana
    [3] => Cherry
 )
 Array
 (
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
 )

 */

                            
4. array_values()

The array_values() function returns an array containing all the values of an array.

Example 1:

 <?php
 
$array_1 = [
	   'Orange' => 100,
	   'Apple' => 200,
	   'Banana' => 300,
	   'Cherry' => 400
];
	  
$array_2 = [
            'tango',
            'uniform',
            'victor',
            'rosy',
            'ray'
];
	
 echo "<pre>";
 
 print_r(array_values($array_1));
 print_r(array_values($array_2));

 ?>

 /*

 Output :

 Array
 (
    [0] => 100
    [1] => 200
    [2] => 300
    [3] => 400
 )
 Array
 (
    [0] => tango
    [1] => uniform
    [2] => victor
    [3] => rosy
    [4] => ray
 )

 */

                            

Other use of array_values() function is to quickly collapse an array that has had some elements unset using unset() into a freshly-ordered indexed array with no blank entries.

Example 2:

 <?php
 
$array_1 = [
	   'Orange' => 100,
	   'Apple' => 200,
	   'Banana' => 300,
	   'Cherry' => 400
];
	  
$array_2 = [
            'tango',
            'uniform',
            'victor',
            'rosy',
            'ray'
];
	
 echo "<pre>";
 
 print_r(array_values($array_1));
 unset($array_1['Apple']);
 print_r(array_values($array_1));

 print_r(array_values($array_2));
 unset($array_2['2']);	  
 print_r(array_values($array_2));

 
				
 ?>

 /*

 Output :

 Array
 (
    [0] => 100
    [1] => 200
    [2] => 300
    [3] => 400
 )
 Array
 (
    [0] => 100
    [1] => 300
    [2] => 400
 )
 Array
 (
    [0] => tango
    [1] => uniform
    [2] => victor
    [3] => rosy
    [4] => ray
 )
 Array
 (
    [0] => tango
    [1] => uniform
    [2] => rosy
    [3] => ray
 )

 */

                            
5. array_unique()

The array_unique() function simply removes duplicate elements from array and returns the resulting array. Key/value relationships are preserved in the resulting array.

Example 1:

 <?php
 
$ax = [
     'a' => 'alpha',
     'b' => 'bravo',
     'c' => 'charlie',
     'd' => 'delta',
     'e' => 'bravo',
     'f' => 'charlie'
];
	    
 echo "<pre>";
 
 print_r($ax);
 print_r(array_unique($ax));
				
 ?>

 /*

 Output :

 Array
 (
    [a] => alpha
    [b] => bravo
    [c] => charlie
    [d] => delta
    [e] => bravo
    [f] => charlie
 )
 Array
 (
    [a] => alpha
    [b] => bravo
    [c] => charlie
    [d] => delta
 )

 */

                            

Example 2:

 <?php
 
$ax = [
     'alpha',
     'beta',
     'gamma',
     'delta',
     'beta',
     'bravo',
     'charlie',
     'alpha',
     'beta',
     'rosy'
];
	    
 echo "<pre>";
 
 print_r($ax);
 print_r(array_unique($ax));
 print_r(array_values(array_unique($ax)));
 
				
 ?>

 /*

 Output :

 Array
 (
    [0] => alpha
    [1] => beta
    [2] => gamma
    [3] => delta
    [4] => beta
    [5] => bravo
    [6] => charlie
    [7] => alpha
    [8] => beta
    [9] => rosy
 )
 Array
 (
    [0] => alpha
    [1] => beta
    [2] => gamma
    [3] => delta
    [5] => bravo
    [6] => charlie
    [9] => rosy
 )
 Array
 (
    [0] => alpha
    [1] => beta
    [2] => gamma
    [3] => delta
    [4] => bravo
    [5] => charlie
    [6] => rosy
 )

 */

                            
6. in_array()

The in_array() function searches through array for an element. If it finds the element then it returns TRUE. Elements match only if they have the same value and type as given element otherwise, only the values need to match. Version:

Example 1:

 <?php
 
$ax = [
     'alpha',
     'beta',
     'gamma',
     'delta',
     'beta',
     'bravo',
     'charlie',
     'alpha',
     'beta',
     'rosy'
];
	    
 echo "<pre>";
 
 if (in_array('beta', $ax)) {
	echo "Value exists";
	} else {
	echo "Value Not exists";
	}

 echo "
"; if (in_array('chaplin', $ax)) { echo "Value exists"; } else { echo "Value Not exists"; } ?> /* Output : Value exists Value Not exists */
About Me
Rahul Yadav

Rahul Yadav

Technical Architect

Kuala Lumpur, Malaysia

Connect Me:     

I'm a Full Stack Web Developer working in a MNC and passionate of developing modern web and mobile applications.

I have designed and developed CodephpOnline & CodephpOnline Wiki platform to expatiate my coding and technology learning experiences.

In my leisure time, I write technical articles on web development such as PHP, Laravel, CodeIgniter, Mediawiki, Linux, Angular, Ionic, ReactJS, NodeJS, AJAX, jQuery, Cloud and more.

Tag Cloud