Array Functions In PHP

Array Functions In PHP

Important PHP Array Functions; Through this tutorial, you will learn the most important and daily useful PHP array functions.

Array is a collection of similar datatype values. One array can contain set different of values. Three types of the array are available In PHP, first is indexed, associative, and multidimensional arrays in PHP.

Now the new version of php is 7.3 launch with many features and bugs improvent(fixed). PHP is most popular progmming lanugage in world. PHP is know as Hypertext Preprocessor. The initiative was the name person home page. Most websites and web apps are developed. It is a server side scripting language. PHP script is excute only server side. It is used to develop Static websites pages or Dynamic websites pages or Web applications

PHP latest version 7.3 new features like :

Flexible Heredoc and Nowdoc Syntax
PCRE2 Migration
Multiple MBString Improvements
LDAP Controls Support
Improved FPM Logging
Windows File Deletion Improvements
Several Deprecations

Array Functions In PHP

This tutorial shows you the most important and useful array functions of PHP and How to use this array function of PHP with example:

Array() Function in PHP

This is simple array function.

<?php
$mobile=array("Samsung","MI","Lenovo");
echo "I like " . $mobile[0] . ", " . $mobile[1] . " & " . $mobile[2] . ".";
?>

Array Unique Function in PHP

As the name suggest array unique function is remove duplicate value in an array.

<?php
$a=array("a"=>"56","b"=>"50","c"=>"56");
print_r(array_unique($a));
?>

Array Difference Function in PHP

This array_diff () function is used know difference between two or more arrays.

 <?php
$a1=array("1"=>"banana","2"=>"apple","3"=>"orange","4"=>"fruits");
$a2=array("1"=>"banana","2"=>"apple","3"=>"fruits");
$result=array_diff($a1,$a2);
print_r($result);
?>

Array Merge Function in PHP

Array_merge() is used to merge two or more array to one array

<?php  
$a1=array("1"=>"banana","2"=>"apple");
$a2=array("1"=>"grapes","2"=>"fruits");
$result=array_merge($a1,$a2);
print_r($result);
?>

Array Random Function in PHP

This function is used to generate random keys from array.

 <?php
$a=array("100","200","500","6000","7000");
$random_keys=array_rand($a,4);
echo $a[$random_keys[0]]."<br>";
echo $a[$random_keys[1]]."<br>";
echo $a[$random_keys[2]]."<br>";
echo $a[$random_keys[3]];
?>

Array Replace Function in PHP

array_replace() function is used to replace the value of arrays.

 <?php
$a1=array("500","560");
$a2=array("1000","2000");
print_r(array_replace($a1,$a2));
?>

Array Reverse Function in PHP

This Array reserse function is reverse the order of array.

<?php
$a=array("a"=>"Volvo","b"=>"BMW","c"=>"Toyota");
print_r(array_reverse($a));
?>

Array Search Function in PHP

This function is used to search a value in array.

<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_search("red",$a);
?>

Array Sum Function in PHP

Array_sum function is used to sum of all value in array.

<?php
$a=array(25,50,75);
echo array_sum($a);
?>

Array Chunk Function in PHP

This array chunk function is mostly used to split an array to chunks.

 <?php 
$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Farari");
print_r(array_chunk($cars,2));
?>

Array Pop Function in PHP

The Array Pop function used to remove element of array.

<?php 
$a=array("5","10","15");
array_pop($a);
print_r($a);
?>

Array Push Function in PHP

The Array Push function used to add new element of array.

<?php 
$a=array("10","15","20");
array_pop($a);
print_r($a);
?>

Conclusion

Through this tutorial, you have learned important php array functions.

If you have any questions or thoughts to share, use the comment form below to reach us.