PHP Arrays

Arrays are very interesting part of PHP programming. And also, they are very powerful compound type in PHP. As like as other programming languages, arrays can be classified as indexed array and associative array. And, it can further be classified as one dimensional and multi dimensional array, based on the level of array index.

In PHP, we can have effective programming practice, if we are expertise in using array related functions. So, let we discuss about some of those function in this article.

php_arrays

  • range(start, end) – This function is used to create an array of continuous numerics or alphabets from start and end values as given as the argument of this function. For example,
    $character_array = range('e','i'); //  creates array('e','f','g','h','i')
  • reset() – This function will move array pointer to the initial position of the array which is taken as an argument of this method.
  • array_slice(array, offset, length) – It is used to crop some specified set of values from an array. Here, the array to be sliced will be the first argument of this function, second argument needs offset from where the array to be sliced and the third argument is the number of values to be sliced. For example,
    $color_array = array("Red","Blue","Green","Yellow","Brown"); 
    sliced_array = array_slice($color_array,1,3);//  now the sliced array will be array("Blue","Green","Yellow")
  • shuffle() – This function has the array as an argument to change the position of its value in random manner.
  • array_chunk() – To break an array into the specified length of chunks, this function is used. For example,
    $character_array = array('e','f','g','h','i');
    $chunks = array_chunk($character_array,2);
    //$chunks will be as follows
    /*
    Array (
    [0] => Array (
    [0] => e
    [1] => f
    )
    [1] => Array (
    [0] => g
    [1] => h
    )
    [2] => Array (
    [0] => i
    )
    )
    */
  • merge() – As per the name of this function, it is for joining more number of arrays.
  • array_key_exists() – This function is used to check whether the given key exists in the array or not. This function takes key element and entire array as arguments of it.
  • array_keys(), array_values() – These two functions will get the keys and values of an associative array respectively. The following example will show how these function will results with the key and value arrays.
    $student_array = array("name"=>"Martin","mark"=>"90","grade"=>"A");
    $key_array = array_keys($student_array) // Will hold the array as : array("name","mark","grade")
    $value_array = array_values($student_array) // Will hold the array as : array("Martin","90","A")
  • array_push()/array_pop()/array_shift()/array_unshift() – array_push() is used to push some values into an array. Whereas, new elements are added at the end of the array array. And then the array_pop() will retrieve the last element of the array. Where as, array_shift and array_unshift method will do the same thing except that the insert and retrieve operations is performed at the beginning of the array.
  • array_splice() – This function is used to remove set of elements from a given array like array_slice. And it can also replace the removed elements with the new set of elements where as the length of the removed set and new array both should be same. For example,
    $character_array =  array('e','f','g','h','i');
    $number_array = array('1','2','3');
    $changed_array = array_splice($character_array,1,3,$number_array); // will hold the element like: array('e','1','2','3','i');
  • extract()/compact() – These two functions operates reverse to each other. The compact() is used to convert the variable into an array, whereas, extract() is used to extract an array and convert the element of that array as variables. The example shows it as follows.
    $student_array = array("name"=>"Martin","mark"=>"90","grade"=>"A");
    extract($student_array,EXTR_PREFIX_ALL,"student"); 
    echo $student_name . "-" . $student_mark . "(" . $student_grade . ")"; // prints as Martin - 90(A)
    $compact_array = compact("student_name","student_mark","student_grade"); // will form an array

    The second and third argument of extract() is optional. It is used to add prefix to the variable name created by this function.

This PHP code tutorial was published on April 15, 2013.

↑ Back to Top