PHP Array Functions With Examples

In this post, We will learn PHP Array Functions With Examples. PHP provide 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.

Read also : Laravel Check if key exists in an Array Example

How to Create Array in PHP

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

In PHP, there are three types of arrays:

  • Indexed arrays – Arrays with a numeric index
  • Associative arrays – Arrays with named keys
  • Multidimensional arrays – Arrays containing one or more array

Read also : Laravel add Array Element at First Position

PHP Array Functions Examples

Convert Array to String 

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

Read Also :  Laravel 7 Socialite Login with Google Account Example

Example : Array to string

$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);

Read also : How to get Random Value from Array in laravel

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);

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