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 end() function

PHP end() function The end() function in PHP sets the internal pointer of an array to its last element. This function returns the value of the last element Syntax end ( array &$array )   Parameter array (required)– This parameter represents the input array. Return This function returns...

1 minute read.

PHP array_uintersect_assoc() Function

PHP array_uintersect_assoc() Function The array_uintersect_uassoc() function in PHP calculates the intersection of arrays by comparing the data and indexes by separate callback functions. It returns an array containing all the values of the first input...

2 minutes read.

PHP nl2br() Function

PHP nl2br() Function The nl2br() function in PHP inserts HTML line breaks before all newlines in a string. Syntax nl2br ( string $string [, bool $is_xhtml] ) Parameter sting(required)- This parameter specifies the input string. is_xhtml(optional)- This parameter states whether to use XHTML compatible line breaks...

1 minute read.

PHP array_chunk() Function

PHP array_chunk() Function The array_chunk() function in PHP is used to split an array into chunks where the last chunk may contain less than size elements. Syntax array_chunk ( array $array , int $size [, bool $preserve_keys ]...

1 minute read.

PHP break

What is Break in PHP? Inside the loop, the break statement is used to control the loop in preprocessor hypertext. PHP utilizes the break keyword to implement the break statement, which...

3 minutes read.

PHP unserialize() Function

PHP unserialize() Function The unserialize() function in PHP takes a single serialized variable and converts it back into a PHP value. Syntax unserialize ( string $str [, array $options ] ) Parameter value(required)- This parameter represents the serialized string. options(optional)- This parameter signifies any options to be...

1 minute read.

PHP metaphone() Function

PHP metaphone() Function The metaphone() function in PHP calculates the metaphone key (representing how a string sounds or pronounced) of a string. This function is used to text search and text matching or spelling...

1 minute read.

How to use Date and Time in PHP

HOW TO USE DATE AND TIME IN PHP: PHP time and date function  informs us about current date and time. it can be manipulated also. How time() function works: The time() function...

2 minutes read.

PHP crypt() Function

PHP crypt() Function The crypt () function in PHP returns a hashed string using DES, Blowfish, or MD5 algorithms. Some constants of crypt() function are as follows: [CRYPT_STD_DES][CRYPT_EXT_DES][CRYPT_MD5][CRYPT_BLOWFISH][CRYPT_SHA_256][CRYPT_SHA_512] etc. Syntax crypt ( string $str [, string $salt ] )  Parameter str(required)- This parameter represents the string to be hashed salt(optional)- This parameter...

2 minutes read.

PHP array_key_last() function

The array_key_last () function in PHP gets the last key of an array if the specified array is not empty. Syntax array_key_last ( array $array )  Parameter array(required)- This parameter signifies the input array. Return This function returns the last key of the...

1 minute read.

PHP array_keys() Function

PHP array_keys() Function The array_keys() function  in PHP is used to return all the keys, numeric and string from the given array. Syntax array_keys ( array $array ) or array_keys ( array $array , mixed $search_value [, bool $strict = FALSE ] )  Parameter array(required)- This parameter specifies the input array search_value(optional)- This parameter specifies a value, where...

1 minute read.

PHP str_repeat() Function

PHP str_repeat() Function The str_repeat() function in PHP repeats a string a specified number of times. Syntax str_repeat ( string $input , int $multiplier ) Parameter string(required)- This parameter signifies the string to be repeated. multiplier(required)- This parameter represents the number of times the string will be...

1 minute read.

PHP doubleval() Function

PHP doubleval() Function The doubleval() function in PHP is used to give the float value of a variable. This function is an alias of floatval(). Syntax doubleval ( mixed $var ) Parameter var(required)- This parameter represents the name of the variable that...

1 minute read.

PHP Data Types

PHP data type is used to hold the various types of data or value.  There are three types of data types in PHP. It is a loosely data typed language, so...

2 minutes read.

PHP trim() Function

PHP trim() Function The trim() Function in PHP strips the whitespace or other characters from the beginning and the end of the given string. The trim() function strips the following characters: " " - an ordinary space."\t"- a tab."\n"- a new...

1 minute read.

PHP str_replace() Function

PHP str_replace() Function The str_replace() function in PHP replaces all occurrences of the search string with the specified replacement string. This function is case-sensitive. Syntax str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) Parameter search(required)- This parameter signifies the value to search. replace(required)-...

1 minute read.

PHP array_key_first() function

The array_key_first () function in PHP gets the first key of an array if the specified array is not empty. Syntax array_key_first(array;$array) Parameter array(required)- This parameter signifies the input array. Return This function returns the first key of the specified...

1 minute read.

PHP Validation

PHP validation is used to check whether the field is filled or not in the proper way by the user. There are two types of validation in PHP. Client Side Validation:...

5 minutes read.

PHP join() Function

PHP join() Function The join() function of PHP returns a string from the elements of an array. This function is an alias of the implode() function. Syntax join(separator,array) Parameter separator(Optional)- Specifies what to put between the array elements. The default...

1 minute read.

PHP array_diff_ukey() Function

The array_diff_ukey() function in PHP compares the keys from array1 against the keys from array2 and returns the difference containing all the entries from array1 that are not present in any of the other arrays. Syntax array_diff_ukey ( array $array1 , array $array2 [, array $... ], callable $key_compare_func ) Parameter array1(required)- This parameter signifies the...

1 minute read.