PHP Array Functions

There are various array functions in PHP. They are used to access and manipulate the elements ofan array. Following are functions of array.
Array Function Description
array() It is used to create an array.
array_change_key_case() It is used to convert lowercase or uppercase of all keys in an array.
array_chunk() It is used to split an array into chunks of arrays.
array_pop() It is used to delete the last element of an array.
array_merge() It is used to merge one or more arrays into one array.
array_rand() It is used to returns one or more random keys from an array.
array_reverse() It returns an array in the reverse order.
array_search() It searches an array for a given value and returns the key.
count() It returns the number of elements in an array.
extract() It imports variables into the current symbol table from an array.
sort() PHP sort() function sorts all the elements in an array.
  • PHP array() function
PHP array()function is used to create  an array( Associate , Index and Multidimensional). Example:
<!DOCTYPE html>
<html>
<body>
<?php
$week=array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$arrlength=count($week);
echo "=====To print Week days using an array======"."<br>";
for($x=0;$x<$arrlength;$x++)
  {
echo $week[$x];
echo "<br>";
  }
?>
</body>
</html>
Output =====To print Week days using an array====== Sunday Monday Tuesday Wednesday Thursday Friday Saturday
  • array_change_key_case() function
PHP array_change_key_case() function is used to change the case of all key of an array. Example
<!DOCTYPE html>
<html>
<body>
<?php
  $salary=array("Sunday"=>"1","Monday"=>"2","Tuesday"=>"3");
print_r(array_change_key_case($salary,CASE_UPPER));
  ?>
</body>
</html>
Output
Array ( [SUNDAY] => 1 [MONDAY] => 2 [TUESDAY] => 3 )
3). PHP array_chunk() function PHP array_chunk() function is  used to split an array into chunks. We can divide array into many parts. Example
<!DOCTYPE html>
<html>
<body>
<?php
  $salary=array("Sunday"=>"1","Monday"=>"2","Tuesday"=>"3");
print_r(array_chunk($salary,2));
  ?>
</body>
</html>
Output
Array ( [0] => Array ( [0] => 1 [1] => 2 ) [1] => Array ( [0] => 3 ) )
PHP array_pop() Function PHP array_pop() function is used to delete last element of an array. Example
<?php
$a=array("red","green","blue");
array_pop($a);
print_r($a);
?>
PHP count() Function PHP count() function is used to return the number of elements in an array. Example
<!DOCTYPE html>
<html>
<body>
<?php
$color=array("Red","Blue","Green","Magenta");
echo count($color);
?>
</body>
</html>
Output 4 PHP sort() Function PHP short() function is used to sort the elements of  an array in ascending order. Example
<!DOCTYPE html>
<html>
<body>
<?php
$color=array(27,21,83,11);
sort($color);
$clength=count($color);
echo"Assending Order [27,21,83,11]"."<br>";
for($x=0;$x<$clength;$x++){
echo $color[$x];
echo "<br>";
  }
?>
</body>
</html>
Output Assending Order [27,21,83,11] 11 21 27 83

Related Topics

PHP str_ireplace() Function

PHP str_ireplace() Function The PHP str_ireplace() function in PHP replaces some characters with some other characters in a string. Syntax str_ireplace ( mixed $search , mixed  $replace , mixed $subject [, int &$count ] ) Parameter search(required)- This parameter specifies the value being searched for. replace(required)- This parameter represents the replacement value...

1 minute read.

PHP array_intersect() function

The array_intersect () function in PHP is used to compute the intersection of arrays. It returns an array containing all the entries from array1 that are present in all the other arrays. Syntax array_intersect ( array $array1 , array $array2 [, array $... ]) Parameter array1(required)- This parameter...

1 minute read.

PHP array_unique() Function

PHP array_unique() Function The array_unique() function in PHP removes the duplicate values from the specified array. Syntax array_unique (array $array [, int $sort_flags = SORT_STRING]) Parameter array(required)- This array represents the first input array. sort_flags (optional)- This parameter is used to modify the sorting behavior. The...

1 minute read.

PHP convert_cyr_string() Function

PHP convert_cyr_string() Function The convert_cyr_string() function in PHP converts from one Cyrillic character-set to another. The supported characters set are given below: k - koi8-rw - windows-1251i - iso8859-5a - x-cp866d - x-cp866m...

1 minute read.

PHP sha1_file() Function

PHP sha1_file() Function The sha1_file() in PHP calculates the sha1 hash of a file and returns the hash (40-character hexadecimal number). Syntax sha1_file ( string $filename [, bool $raw_output] )  Parameter filename(required)- This parameter represents the filename of the file to hash. raw_output(optional)- This...

1 minute read.

PHP print() Function

PHP print() Function The print() function in PHP is used to output one or more string. Syntax print ( string $arg ) Parameter arg(required)- This parameter represents the input data. It can take one or more arguments. Return This function returns an integer value 1. Example...

1 minute read.

PHP substr_compare() Function

PHP substr_compare() Function The substr_compare() function in PHP compares two strings from a specified start position. It is a binary-safe and optionally case-sensitive function. Syntax substr_compare ( string $main_str , string $str , int $offset [, int $length [, bool$case_insensitivity]] )  Parameter main_str(required)- This parameter specifies the first string to compare. str(required)- This parameter...

1 minute read.

PHP shuffle() Function

PHP shuffle() Function The shuffle() function in PHP shuffles an array in a random array. Syntax shuffle ( array &$array )  Parameter array(required)- This parameter represents the input array.  Return This parameter returns a Boolean value TRUE on success or FALSE on failure. Example 1 Output Original Array: Array ...

1 minute read.

PHP sizeof() Function

PHP sizeof() Function The sizeof() function in counts all elements and return the size of the array. This function is an alias of count(). Syntax sizeof( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )  Parameter array_or_countable(required)– This parameter represents the input array or countable object. mode(optional)-...

1 minute read.

PHP Echo And Print

In PHP, echo and print is used to get output data to the screen but there is some difference, between echo and print. PHP echo PHP echo is used to display the...

1 minute read.

PHP rsort() Function

PHP rsort() Function The rsort () function in PHP is used to sort an array in reverse order i.e., highest to lowest. Syntax rsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) Parameter array(required)- This parameter represents the input array. sort_flags (optional)- This parameter is used to...

1 minute read.

PHP levenshtein() Function

PHP levenshtein() Function The levenshtein() function in PHP calculates the Levenshtein distance between two strings. Syntax levenshtein ( string $str1 , string $str2 , int $cost_ins , int $cost_rep , int $cost_del )  Parameter str1(required)- This parameter signifies one of the strings being evaluated for Levenshtein distance. str2(required)- This parameter signifies one of the strings being evaluated...

1 minute read.

PHP array_intersect_key() function

The array_intersect_key () function in PHP is used to compute the intersection of arrays using keys for comparison. It returns an array containing all the entries from array1 which have keys that are present in...

1 minute read.

PHP String get_html_translation_table() Function

The get_html_translation_table() function in PHP is used to return the translation table used by the htmlentities() and htmlspecialchars() functions. Syntax get_html_translation_table ([ int $table = HTML_SPECIALCHARS [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = "UTF-8" ]]] ) Parameter table (optional)- This parameter specifies which translation table to return. It...

2 minutes read.

PHP sort() Function

PHP sort() Function The sort() function in PHP sorts an array where the elements will be arranged from lowest to highest. Syntax sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )  Parameter array(required)- This parameter represents the input array. sort_flags (optional)- This parameter is used to modify the...

1 minute read.

PHP Array Functions

There are various array functions in PHP. They are used to access and manipulate the elements ofan array. Following are functions of array. Array Function Description array() It is used to create an array. array_change_key_case() It is...

3 minutes read.

PHP krsort() Function

PHP krsort() Function The krsort() function in PHP sorts an array by key in reverse order while maintaining the keys. Syntax krsort (array & $array[,int $sort_flags= SORT_REGULAR] ) Parameter array(required)- This parameter represents the input array. sort_flags(optional)- This...

3 minutes read.

PHP Arrays

PHP Array is usedto hold more than one value at a time.  In another words, we can say that an array stores multiple values in one single variable. Following are the...

2 minutes read.

PHP substr_replace() Function

PHP substr_replace() Function The substr_replace() function in PHP replaces a part of a string with another string. Syntax substr_replace (mixed $string , mixed $replacement , mixed $start [, mixed $length ] ) Parameter string(required)- This parameter represents the input string. replacement(required)- This parameter signifies the string to replace with. start(required)-...

2 minutes read.

PHP strnatcmp() Function

PHP strnatcmp() Function The strnatcmp() function in PHP compares two strings using a "natural" algorithm. It is case-sensitive. Syntax strnatcmp ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less than(...

1 minute read.