Sunday 5 February 2017

Most Useful PHP Array Functions



Arrays are used to store and perform operations on a set of values in as form of simple or multidimensional arrays. Following are few most commonly used functions which are used to manipulate arrays in various ways.


  • count : Syntax :  count($array, $mode); 
                 It is used to get the number of elements in an array
  1. $array : Required, Specifies the array.
  2. $mode : Optional. Specifies the mode.
            0 - Default. Does not count all elements of multidimensional arrays
            1 - Counts the array recursively (counts all the elements of multidimensional arrays)
  • sort : Syntax :  sort($array, $sort_type_flags);       
    1. $array : Required value which specifies the array to be sorted.
    2. $sort_type_flags : It is used to modify the sorting behavior.

    The optional second parameter sort_type_flags may be used to modify the sorting behavior (comparison of array elements/items) using below values:

    • SORT_REGULAR - Default - Compare items normally (don't change types)
    • SORT_NUMERIC - Compare items numerically
    • SORT_STRING - Compare items as strings
    • SORT_LOCALE_STRING - Compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
    • SORT_NATURAL - Compare items as strings using "natural ordering" like natsort()
    • SORT_FLAG_CASE - Can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively


  • array_keys :   Syntax: array_keys($array, $value = NULL, $strict = FALSE);
          Return an array of all or a subset of keys of a given array
  1. $array : Required value which specifies the array to be used.
  2. $value : Optional, if specified then only the keys which are matching the value will be returned.
  3. $strict : Optional, if passed true then the type of value specified will also be checked i.e (=== comparison operator will be used). 

  •  array_values : Syntax : array_values($array);
          Returns an array containing all the values of a given array and indexes the array numerically.
  1. $array :  Required specifying an array to be used. 

  •  array_flip : Syntax :  array_flip($array);
           Exchanges all the keys with their associated values in a given array and returns the flipped array on success and NULL on failure.
  1. $array : Required specifying the array which needs to be flipped.

  •  array_combine : Syntax : array_combine($keys, $values);
           Create an array by using key elements from one array and values from another array as the corresponding values (Both arrays must have equal number of elements).
  1.  $keys : Rquired array of keys to be used.
  2. $values : Required array of values to be used.

  •  array_merge : Syntax : array_merge($array1, $array2, $array3...)
Merge one or more arrays into one array (If two or more array elements have the same key, the last one overrides the others.) 
  1. $array1  : Required specifying an array to be used.
  2. $array2 : Optional specifying an array to be used.
  3. $array3 : Optional specifying an array to be used.

No comments:

Post a Comment