PHP MySQL tutorial

Commonly Used Array Functions in PHP

Arrays are used to store collection of items. We already know how to create an array and how to add and retrieve items from the array. PHP contains myriad of built-in functions that can be used to manipulate arrays. This article explains some of these functions.

array_values($array)

This function takes array as a parameter and return all the values that are present in the associative array.

array_keys($array)

This function takes array as a parameter and returns keys from the associative array.

sort($array)

This function takes array as a parameter and sorts the array items in the ascending order by default. Strings are sorted in the ascending alphabetical order.

array_reverse($array)

This function takes array as a parameter and returns the array items in the reverse order.

array_rand($array)

This function takes array as a parameter and returns any randomly selected item from the array.

array_pop($array)

This function takes array as a parameter and removes the last element from the array.

array_push($array, $item)

This function takes array and item as parameters and inserts the specified item at the end of the array.

array_key_exists($keytofind, $array)

This function takes key and array as parameters and return true if the specified key exists in the array.

in_array($valuetofind, $array)

This function takes value and array as parameters and return true if the specified value exists in the array.

sizeof($array)

This function takes value and array as parameters and return the number of items in the array.

Lets have a look at a working example of the aforementioned functions.

'Mike',
        'Age'=>'25',
        'Gender'=>'Male',
        'Nationality'=>'American'
    );

    $simplearray = array("stacey","Jacob","Adam","Bran");
    $numarray = array(10,43, 54,23,12, 65,72);
        
    // will print all values in $array
    print_r(array_values($array));
    echo "
"; // will print all keys in $array print_r(array_keys($array)); echo "
"; // will sort items in the array $simplearray in alphabetical order sort($simplearray); print_r($simplearray); echo "
"; // will reverse $numarray $reversed = array_reverse($numarray); print_r($reversed); echo "
"; // will select a random number from $simplearray $rand = array_rand($simplearray); echo $simplearray[$rand]; echo "
"; // will remove last element from array array_pop($simplearray); print_r($simplearray); echo "
"; // will add value after last array index array_push($simplearray, "Elizabeth"); print_r($simplearray); echo "
"; // will check if key exists $gender = array_key_exists("Gender", $array); if($gender) {echo "Gender key exists in associative array
";} // will check if Mike exists in associative array $name = in_array("Mike", $array); if($name) { echo "Mike value exists in associative array
";} // will check the number of items in $numarray echo sizeof($numarray); ?>

Download the code
Run the code

<<< Sorting Arrays in PHPCommonly Used String Functions in PHP >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .