PHP 7 Array Functions

Laravel

70

w3alert.com

29-01-2020


PHP Array Functions

PHP provides various types of Array Functions to access and manipulate the Elements/values of an array.

The following some important and daily useful Array functions are given below:

PHP array() function

The PHP array() function is used to create an array.

There are three types of PHP array() function.

Indexed arrays: which is used to hold items/elements in linear order. the index starts with 0 and items/elements of the array are accessed by the index with the number.

Associative arrays: It also acts like indexed arrays but it stores the items/elements of the array in key-value pairs.

Multidimensional arrays: It is an array that contains more than one array in the same array. as the name suggests, each element in the array holds the other sub-arrays.

you can learn more about PHP arrays, so visit this link: https://w3alert.com/php-tutorial/php-arrays-tutorial

Syntax: 

    array( value1, value2, value3 );

    OR

    array( key1 => value1, key2 => value2, key3 => value3 );

Example of PHP array() function

Write the program to create an array with the PHP array() function given below

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    // assigned a variable to create an array
    $arr =  array( "PHP", "PYTHON", "JAVA" );

    // Print an array
    print_r( $arr );

    ?>

    </body>
    </html>

PHP array_reverse() function

The array_reverse() function is built-in function of PHP. which is used to reverse the order elements of an array.

There are two parameters of the PHP array_reverse() function.

Given_array: array to divide.

Preserve_keys: Preserve keys that take boolean values. If you apply TRUE array_reverse() function then the preservation of keys of the array otherwise By default the value of this parameter is taken as FALSE.

Syntax:

    array_reverse( $array, preserve_keys );

Example of PHP array_reverse() function

Let's create a program to reverse an array with PHP array_reverse() function.

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    // Create an array
    $framework = array( "WEB2PY","DJANGO", array( "LARAVEL","CODEIGNITER" ) );

    print_r( $framework ); // Print an array.

    echo "<br>";

    print_r(array_reverse( $framework )); // Print without apply preserve_keys.

    echo "<br>";
     
    print_r(array_reverse( $framework,true )); // Print with apply preserve_keys.

    ?>

    </body>
    </html>

PHP array_rand() function

The array_rand () function is a predefined function in PHP. Which is used to get multiple types of elements from a given array.

Syntax:

    array_rand( $array, rand_keys );

Example of PHP array_rand() function

Let's create a program to get multiple types of elements with PHP array_rand() function.

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    // Create an array
    $lang = array( "PHP","PYTHON","JAVA","JAVASCRIPT" );

    $cal = array_rand( $lang,2 ); // method to get elements.

    print_r( $cal ); // Print elements numeric.

    echo "<br>";

    // Print elements by given keys.
    echo $lang[ $cal[0] ] ."<br>" ;
    echo $lang[ $cal[1] ] ."<br>" ;

    ?>

    </body>
    </html>

PHP array_intersect() function

The PHP array_intersect () function is used to compare two or more arrays and displays the matches array elements.

Syntax:

   array_intersect( $array1, $array2, $array3 );

Example of PHP array_intersect() function

Let's create a program to display match in given arrays with PHP array_intersect() function.

    <!DOCTYPE html>
    <html>
    <body>

    <?php
    //Create arrays
    $arr1 = array( "Davexy","Jasone","Melissa","Margean" );
    $arr2 = array( "Margean","Endro","Melissa" );

    print_r(array_intersect( $arr1,$arr2 )); // Print the match.

    ?>

    </body>
    </html>

PHP sizeof() function

The PHP sizeof () function is used to count number of items / elements in given an array.

There are two modes in PHP sizeof() function

  •     0       It is a default mode if you do not set mode. the sizeof() function automatically set 0 and this function does not count recursively. it is used with  single-dimensional arrays
  •     1       If you will set 1. The sizeof() function counts recursively. it is used with multidimensional arrays.

Syntax:

    sizeof( $array, mode );

Example of PHP sizeof() function

Let' create program to count number of items/elements in an array with PHP sizeof() function.

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $employee = array( "John"=>array( "PHP", "LARAVEL" ), "Emma"=>array ( "JAVA", "JAVASCRIPT" ) ) ;

    echo sizeof( $employee ) ."<br>" ; // Do not recursive count

    echo sizeof( $employee,1 ) ."<br>"; // Recursive count

    ?>

    </body>
    </html>

PHP count() function

The PHP count () function act like PHP sizeof() function. It is used to count number of items / elements in given an array.

There are two modes in the PHP count() function

  •     0    It is a default mode if you do not set the mode in count() function automatically set 0 and this function does not count recursively. it is used with single-dimensional arrays
  •     1    If you will set 1 in count() function counts recursively. it is used with multidimensional arrays.

Syntax:

    count( $array, mode );

Example of PHP count() function

Let' create program to count number of items/elements in an array with PHP count() function.

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $employee = array( "John"=>array( "PHP", "LARAVEL" ), "Emma"=>array ( "JAVA", "JAVASCRIPT" ) ) ;

    echo count( $employee ) ."<br>" ; // Do not recursive count

    echo count( $employee,1 ) ."<br>"; // Recursive count

    ?>

    </body>
    </html>

PHP is_array() function

The PHP is_array() function is used to check is array or not. If the given array is an array, it returns true(1) otherwise it returns false/nothing.

Syntax:

    is_array( $variable );

Example of PHP is_array() function

Let's create a program to check an array with PHP is_array() function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    // assigned three variables to check is array or not
    $var1 = " Welcome to w3alert.com";
    $var2 = array("Agada"=>"28", "Beom"=>"33", "Juan"=>"46");
    $var3 = "Joshua, Alfredo, Dimy";

    // Print the check array
    echo is_array($var1) . "<br>";
    echo is_array($var2) . "<br>";
    echo is_array($var3) . "<br>";
    ?>

    </body>
    </html>

PHP array_merge() function

The PHP array_marge() function is used to merge more than one array and return a new array.

Syntax:

   array_merge( $array1, $array2, $array3 );

Example of PHP array_merge() function

Let's create a program to merge multiple arrays with PHP array_merge() function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    // Create arrays
    $framwork1 = array( "Laravel","Codeigniter" );
    $framwork2 = array( "Django","Web2py" );

    print_r(array_merge( $framwork1, $framwork2 )); // arrays merge and print a new array.

    ?>

    </body>
    </html>

PHP in_array() function

The PHP in_array () function is used to check whether a given array contains a given value. It will return true if the value exists in the array. Otherwise, it will return False.

Syntax:

    in_array( value, $array , mode );

Example of PHP in_array() function

Let's create a program to check value in an array with PHP in_array() function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    // Create an array
    $name = array("Ronan", "Brayden", "Hugo", "Diego", "Antonio");

    // Method to check value in an array.
    if (in_array( "Hugo", "Ronan", $name ))
      {
      echo "The names are in an array.";
      }
    else
      {
      echo "The names are not in an array.";
      }
    ?>

    </body>
    </html>

PHP array_keys() function

The PHP array_keys () function is used to derive all keys or subset of the keys from given an array.

Syntax:

    array_keys( $array );

Example of PHP array_keys() function

Let's create a program to check value in an array with PHP array_keys() function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    // create an associative array
    $info = array( "John" => "PHP Developer", "Max"=>"JAVA Devloper", "Alvish"=>"PYTHON Developer" ) ;

    print_r(array_keys( $info )) ; // Print all keys of an array. 
    ?>

    </body>
    </html>

PHP array_pop() function

array_pop() function is a built-in function in PHP which is used to remove the last value/element from the given array.

Syntax:

    array_pop( $array );

Example of PHP array_pop() function

Let's create a program to remove the last element in given array with PHP array_pop() function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    // Create an array
    $employee = array( "Steffan","Devin","Aaron" ) ;

    array_pop( $employee ); // Remove the last element from given array.

    print_r( $employee ); // Print the array.

    ?>

    </body>
    </html>

PHP array_push() function

array_push() function is a built-in function in PHP that is used to push more than one value/element into the given array. and these values/elements insert to the end of the given array.

Syntax:

    array_push( $array, push_value1, push_value2 );

Example of PHP array_push() function

Let's create a program to push elements into the given array with PHP array_push() function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    // Create an array
    $employee = array( "Steffan","Devin","Aaron" ) ;

    array_push( $employee, "Ronan", "Marco" ); // Push elements into the given array.

    print_r( $employee ); // Print the array.

    ?>

    </body>
    </html>

PHP array_shift() function

The PHP provides array_shift function which is used to remove the first value/element from the given array.

Syntax:

   array_shift( $array );

Example of PHP array_shift() function

Let's create a program to remove the first element from the given array with PHP array_shift() function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    // Create an array
    $employee = array( "Steffan","Devin","Aaron" ) ;

    array_shift( $employee ); // remove the first element from the given array.

    print_r( $employee ); // Print the array.

    ?>

    </body>
    </html>

PHP array_unshift() function

The PHP provides an array_unshift() function which is used to insert more than one value/element into the given array. and these values/elements insert to the beginning of the given array.

Syntax:

   array_shift( $array, insert_value1, insert_value2 );

Example of PHP array_unshift() function

Let's create a program to insert elements into the given array with PHP array_unshift() function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    // Create an array
    $employee = array( "Steffan","Devin","Aaron" ) ;

    array_unshift( $employee, "Ronan", "Marco" ); // Insert elements into the beginning of given array.

    print_r( $employee ); // Print the array.

    ?>

    </body>
    </html>

PHP array_replace() function

This function replaces the values of the first array with the values of other arrays.

Syntax:

    array_replace( $array, $replace_array1, $replace_array2 );

Example of PHP array_replace() function

Let's create a program to replace value of first array with PHP is_array() function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    // Create first array
    $lang1 = array("Javascript",".net");

    // Create other arrays
    $lang2 = array("0" => "PHP", "2" => "Ruby"); 
    $lang3 = array("3" => "c#", "1" => "Python"); 

    // Print replaced array
    print_r(array_replace($lang1,$lang2,$lang3)); 

    ?>

    </body>
    </html>

PHP array_values() function

The PHP array_values ​​() function is used to derive an array of values ​​from another array in which key-value or simple values ​​are found.
PHP array_values ​​() creates another array in which it stores all the values ​​and by default assigns numeric keys to the values.

Syntax:

    array_vaalues( $array );

Example of PHP array_values() function

Let's create a program to create another array with PHP is_array() function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $languages = array("server_site1" => "PHP", "Programming" => "PYTHON", "server_site2" => "SQL");

    print_r(array_values($languages));

    ?>

    </body>
    </html>

PHP array_change_key_case() function

PHP array_change_key_case() function is used to change all keys of an array to uppercase or lowercase. 

  • If you do not set the case in array_change_key_case() function, it will automatically set lowercase.
  • If you will set uppercase in array_change_key_case() function changes all keys of an array in uppercase.

Syntax:

    array_change_key_case( $array, case );

Example of PHP array_change_key_case() function

Let's create a program to change all keys of an array to case with PHP array_change_key_case() function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    // Create an array
    $info = array("John"=>"28","Wally"=>"31","Elias"=>"36");

    print_r(array_change_key_case( $info ));  // Print Keys without set case.

    echo "<br>";

    print_r(array_change_key_case( $info,CASE_UPPER ));  // Print Keys with set case.

    ?>

    </body>
    </html>

PHP array_chunk() function

The PHP array_chunk () function is a built-in function in PHP. It is used to divide arrays into arrays with an element of size. The last part may have reduced size elements.

There are three parameters to access PHP array_chunk() function

given_array: array to divide.

size: the size of each part.

preserve_keys: Preserve keys that take boolean values. If you pass TRUE array_chunk() function then the keys are preserved otherwise the chunk is indexed start from 0.

Syntax:

    array_chunk( $array, size, preserve_keys );

Example of PHP array_chunk() function

Let's create a program to the chunk of an array with PHP array_chunk() function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    // Create an array
    $cars=array( "PHP","PYTHON","JAVA","JAVASCRIPT","LARAVEL","CODEIGNITER" );

    print_r(array_chunk( $cars,1 )); // Print chunk of array without set Preserve key.

    echo "<br>";

    print_r(array_chunk( $cars,1,1 )); // Print chunk of array with set Preserve key.
    ?>

    </body>
    </html>