PHP Array Functions provides many functions to access and manipulate the array element. We have covered the various PHP array functions. Because there are many Array functions comes in PHP.
PHP array Functions
PHP array() function creates and returns an array. So we can create the indexed, associative and multidimensional arrays. But we make sure about the syntax and all.
Indexed Array: These are basic arrays with numeric index.
Example :
<?php
$arr = array ("JAVA", "PHP", "LARAVEL");
echo $arr[0];
?>
Output
JAVA
Associative Array: These are key-value pair arrays which have the numeric or text key as index.
Example
<?php
$associate_arr = array (
"Name" => "Admin",
"Post" => "Developer",
"Company" => "Developer Helps"
);
echo $associate_arr ["Name"];
?>
Output
Admin
Multidimensional Array: These are arrays which contain one or more arrays.
Example :
<?php
$multi_arr = array (
array ("JAVA", "JSP", "JSTL"),
array ("PHP", "LARAVEL", "WORDPRESS"),
array ("PYTHON", "DJANGO")
);
echo $multi_arr[0][1];
?>
Output
JSP
PHP Count Function
PHP count() function counts the total number of elements in the array.
Example
<?php
$lang=array("JAVA","PHP","PYTHON");
echo count($lang);
?>
Output
3
PHP Sort Function
PHP sort() function sorts the elements of array.
Example :
<?php
$lang=array("JAVA","PHP","C");
sort($lang);
foreach( $lang as $l )
{
echo "<br>".$l;
}
?>
Output
C
JAVA
PHP
PHP array_reverse function
PHP array_reverse() function returns the array which contain its elements in reversed order.
Example :
<?php
$lang=array("JAVA","PHP","C");
$reverse_lang = array_reverse($lang);
foreach( $reverse_lang as $rev )
{
echo "<br>".$rev;
}
?>
Output
C
PHP
JAVA
PHP array_search Function
PHP array_search() function search the value in an array. This function returns the key if search is successful.
Example
<?php
$arr=array("JAVA","PHP","C");
$key = array_search("PHP",$arr);
echo $key;
?>
Output
1
PHP sizeof Function
PHP sizeof () function returns the size of array or number of elements exist in the array.
Example :
<?php
$arr=array("JAVA","PHP","C");
echo "Size of array is: ". sizeof($arr);
?>
Output
Size of array is: 3
PHP is_array function
PHP is_array() function check the variable in form of array or not. If variable is an array, it returns true otherwise false.
Example
<?php
$lang=array("JAVA","PHP","C");
echo is_array($lang) ? "This is Array" : "This is not Array";
$lang="PHP";
echo is_array($lang) ? "This is Array" : "This is not Array";
?>
Output
This is Array
This is not Array
PHP in_array Function
PHP in_array() function check the specified value is present in array or not. If value is present in an array, it returns true otherwise false.
Example
<?php
$lang=array("JAVA","PHP","C");
$value = "PHP";
echo in_array($value,$lang) ? "This value is Exist" : "This value is not Exist";
?>
Output
This value is Exist
PHP array_merge function
PHP array_merge() function combine two different array in the single array. We will merge an indexed array and an associate array.
Example
<?php
$arr = array ("JAVA", "PHP", "LARAVEL");
$associate_arr = array (
"Name" => "Admin",
"Post" => "Developer",
"Company" => "Developer Helps"
);
$merge_array = array_merge($arr, $associate_arr);
print_r($merge_array);
?>
Output
Array ( [0] => JAVA [1] => PHP [2] => LARAVEL [Name] => Admin [Post] => Developer [Company] => Developer Helps )
PHP array_push function
PHP array_push() function add the new element at the end of array.
Example
<?php
$lang=array("JAVA","PHP","C");
array_push($lang,"C#");
print_r($lang);
?>
Output
Array ( [0] => JAVA [1] => PHP [2] => C [3] => C# )
PHP array_pop Function
PHP array_pop() function remove the last element from an array. This function is opposite of the array_push() function. But we can easily understand with the following example.
Example
<?php
$lang=array("JAVA","PHP","C");
array_pop($lang);
print_r($lang);
?>
Output
Array ( [0] => JAVA [1] => PHP )
More Related Post
- PHP MVC Framework
- Login Page in PHP
- PHP String Replace Function
- PHP For Loop
- PHP Interview Questions 2019
Thanks for the reading post. I hope you like and understand the post. If you have any doubt regarding this post please comment below. Because it helps to improve content quality.