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

Python if-else

Python if-else The else statement is associated with if statement. An else statement executes if the conditional expression in if the statement becomes false or a Zero value. If the conditional expression...

2 minutes 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 Magic Constants

There are various predefine PHP Magic Constants. It starts with underscore(__) and ends with double underscore.  Magic Constant Description __LINE__ To check current line number of the file. __FILE__ To check full path and filename of...

1 minute read.

PHP Variables

PHP Variable is the name of memory location used to hold the data.  In PHP, variable starts with $ symbol. Rules for PHP Variables. Variable starts with $ sign. Variable name...

2 minutes read.

PHP ord() Function

PHP ord() Function The ord() function in PHP converts the first byte of a string to a value between 0 and 255. This function interprets the binary value of the first byte of string as an...

1 minute read.

PHP is_real() Function

The PHP is_real () function in PHP is used to find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter...

1 minute read.

PHP strval() function

PHP strval() function The strval() function in PHP is used to get the string value for the specified variable. This function cannot be used on arrays or objects, if applied then would return only the...

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 str_getcsv() Function

PHP str_getcsv() Function The str_getcsv() function in PHP parses a CSV string into an array and returns an array containing the fields read. Syntax str_getcsv ( string $input [, string $delimiter [, string $enclosure [, string$escape ]]] ) Parameter input(required)- This parameter represents the string to parse. delimiter(optional)- This parameter signifies...

1 minute read.

PHP strspn() Function

PHP strspn() Function The strspn() function of PHP finds the length of the initial segment of string that contains only characters from the mask parameter. Syntax strspn(string$subject,string$mask[,int$start[,int$length]] ) Parameter subject(required)- This parameter represents the input string. mask(required)- This parameter specifies the characters to find. start (optional)- It signifies the position in the string to start...

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 is_bool() Function

The is_bool() Function in PHP is used to  find whether a variable is a Boolean or not. It returns a Boolean value true if the parameter var is a Boolean else it returns...

1 minute read.

PHP htmlspecialchars() Function

PHP htmlspecialchars() Function The htmlspecialchars() function in PHP converts some special predefined characters to HTML entities. The special predefined characters are as follows: & (ampersand) OR &amp;" (double quote) OR &quot;' (single quote) OR &#039;<...

2 minutes read.

PHP next() Function

PHP next() Function The next() function in PHP advances or moves the internal array pointer one place forward and returns the next array value. Syntax next ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns the next array value...

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 String

PHP string is a sequence of characters. It is used to store and manipulate text. We can specify a string literal in four ways: single quoted double quoted heredoc syntax ...

2 minutes read.

PHP array_diff() Function

The array_diff () function in PHP compares array1 against other specified arrays and returns the values in array1 that are not present in any of the other arrays. Syntax array_diff(array$array1,array$array2[,array$...] ) Parameter array1(required)- This parameter signifies the input array to...

1 minute read.

PHP strrchr() Function

PHP strrchr() Function The strrchr() function in PHP finds the last occurrence of a character in the specified string and returns the portion of haystack, which starts at the last occurrence of the needle and goes until the end of haystack....

1 minute read.

PHP array_product() Function

PHP array_product() Function The array_product() function in PHP calculates the product of values in an array. Syntax array_product ( array $array ) Parameter array(required)- This parameter represents the input array. Return This function returns the product of the array elements as an integer or float....

1 minute read.

PHP sha1() Function

PHP sha1() Function The sha1() in PHP calculates the SHA-1 hash of a string. Syntax sha1 ( string $str [, bool $raw_output = FALSE ] )  Parameter str (required)- This parameter represents the input string. raw_output(optional)- This parameter takes Boolean value and returns the digest in raw binary format...

1 minute read.