PHP Array Functions

PHP Array Functions allow you to access and manipulate arrays. Simple and multi-dimensional arrays are supported.

Some Important Functions of PHP Arrays are listed below:

array()

Returns an array of the parameters. The parameters can be given an index with the => operator

Syntax

array(key1 => value1, key2 => value2…)

Parameters
  • key-Optional. Specifies the key, of type numeric or string. If not set, an integer key is generated, starting at 0
  • value-Required. Specifies the value
  • Run : http://ideone.com/7bpwAw

    sizeof ()

    This function is used to count the items of the array. (count() is also used instead of this).

    Run : http://ideone.com/2wkVF6

    array_merge()

    Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one.

    Syntax

    array array_merge ( array $array1 [, array $array2 [, array $array3…]] );

    Parameters
  • array1-Required. Specifies an array.
  • array2-Optional. Specifies an array.
  • array3-Optional. Specifies an array.
  • Run : http://ideone.com/Q1tfcO

    count()

    Count elements in an array, or properties in an object.

    Syntax

    count($array, $mode );

    Parameters
  • array-Required. Specifies an array
  • mode-Optional. Specifies the mode of the function.
  • Run : http://ideone.com/TVH8PK

    array_chunk()

    Chunks an array into size large chunks. The last chunk may contain less than size elements.

    Syntax

    array array_chunk ( array $input, int $size [, bool $preserve_keys] );

    Parameters
  • input-The array to work on
  • size-The size of each chunk
  • preserve_keys-When set to TRUE keys will be preserved. Default is FALSE which will reindex the chunk numerically
  • Run : http://ideone.com/icVJAd

    array_unique

    The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed.

    Syntax

    array_unique ( $array );

    Parameters
  • array1-Required. Specifies an array.
  • Run : http://ideone.com/bCzu4S

    Sort Functions For Arrays

    sort()

    sort arrays in ascending order

    Run : http://ideone.com/epYYuF

    rsort()

    sort arrays in descending order

    Run : http://ideone.com/XIUr2f

    asort()

    sort associative arrays in ascending order, according to the value

    Run : http://ideone.com/EThDYb

    ksort()

    sort associative arrays in ascending order, according to the key

    Run : http://ideone.com/W47oLf

    arsort()

    sort associative arrays in descending order, according to the value

    Run : http://ideone.com/9YtqRL

    krsort()

    sort associative arrays in descending order, according to the key

    Run : http://ideone.com/g6bsJZ


    Congratulations! Chapter Finished. Do you want to practice more?
    Exercises & Assignments
  • No Content Found.
  • Interview FAQs
  • No Content Found.