In this post, We will learn PHP Array Functions With Examples. PHP provides more than PHP inbuilt array functions. I will explain step-by-step array functions with examples in PHP. PHP array functions to you can easy to handle your array in PHP website.


How to Create Array in PHP

In PHP you can create array using array() function.


In PHP, there are three types of arrays:

  1. Indexed arrays – Arrays with a numeric index
  2. Associative arrays – Arrays with named keys
  3. Multidimensional arrays – Arrays containing one or more array

Examples


Convert Array to String 

In PHP you can convert array to string using implode() function.

$fruits = array("Apple", "Banana", "Orange");
$data = implode(',', $fruits);



Convert String to Array 

In PHP you can convert string to array using explode() function.

$fruits = "Apple,Banana,Orange";
 $data = explode(',', $fruits);


Count Number of items in a Array 

In PHP count() function to count number of items in array.

$names = array("Ved","Shlok","Sanket","Fenil");
echo count($names);


Check if a specific key exist

In PPH array_key_exists() function to you can check if specific key exists in a array.

$array = array("name" => "Ved","age" => 21);
echo array_key_exists('name',$array)


Remove and preserve keys

In PHP unset() function to remove key or element in a array.

$array = array("name" => "Ved","age" => 21);
unset($array['name']);
print_r($array);


I hope you understand PHP array functions and it can help you…