PHP Array Functions With Example
Tags: laravel, php, array, function
Published: 2022-07-04 03:27:07

In this article, we will see PHP array functions with example. An array is a data structure that contains a group of elements. Typically these elements are all of the same data type, such as an integer or string. PHP array are commonly used in computer programs to organize data so that a related set of values can be easily sorted or searched. An array can hold many values under a single name, and you can access the values by referring to an index number.

So, let's see PHP array, PHP functions, PHP string, PHP in array, PHP in_array, PHP array to string, PHP string to array, array functions, PHP array_merge, PHP array_splice, PHP array_key_exists, PHP array_search, PHP array_filter
Create an Array in PHP

In PHP, the array() function is used to create an array

---
 
**_[Read Also : Scrolla - jQuery Plugin for Reveal Animations](https://techsolutionstuff.com/post/scrolla-jquery-plugin-for-reveal-animations)_**

---
 
> array();

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 arrays

```
//Array declaration
$names = ['Techsolutionstuff', 'Tech', 'Techsolution', 'Stuff'];
```
 
```
//add to array
$names[] = 'Techsolutionstuff';
```
 

> Array merge :

```
// Array merge
$array3 = array_merge($array1, $array2);
```
 
---

**_[Read Also : How To Validate URL In PHP With Regex](https://techsolutionstuff.com/post/how-to-validate-url-in-php-with-regex)_**

--- 

> Array to string :

```
// Array to string
$text = implode(', ', $names); // 'Techsolutionstuff, Tech, Techsolution, Stuff'
```
 

> String to Array :

```
// String to Array
echo explode(',', $text); // ['Techsolutionstuff', 'Tech', 'Techsolution','Stuff']
```
 

> Remove and preserve keys :

```
// Remove and preserve keys
unset($names[1]); 
// It is now => [0 => 'Techsolutionstuff', 2 => 'Techsolution']
```

--- 

**_[Read Also : Laravel 8 Mobile Number OTP Authentication using Firebase](https://techsolutionstuff.com/post/laravel-8-mobile-number-otp-authentication-using-firebase)_**

---
 

> Remove and change keys :

```
// Or remove and change keys
array_splice($names, 1, 1);
// It is now => [0 => 'Techsolutionstuff', 1 => 'Techsolution']
```
 
> Number of items in a Array :

```
// Number of items in a Array
echo count($names);
```
 

> Associative array :

```
//Associative array:
$person = ['age' => 25, 'gender' => 'men'];
```
 

> Check if a specific key exists:

```
// Check if a specific key exist
echo array_key_exists('age', $person);
```

---
 
**_[Read Also : Google Line Chart Example in Laravel 8](https://techsolutionstuff.com/post/google-line-chart-example-in-laravel-8)_**

--- 

> Array filter :

```
//Array filter (return a filtered array)
$filteredData = array_filter($people, function ($person) {
    return $person->active;
});
```
 

> Search all values:

```
// search all value in the 'name' column
$found_key = array_search('men', array_column($gender, 'name'));
// return 2
```
 
---

You might also like :

- **_[Read Also : Carbon Add Hours In Laravel Example](https://techsolutionstuff.com/post/carbon-add-hours-in-laravel-example)_**

- **_[Read Also : Laravel 8 Socialite Login With Google Account](https://techsolutionstuff.com/post/laravel-8-socialite-login-with-google-account)_**

- **_[Read Also : How to Integrate Razorpay Payment Gateway in Laravel](https://techsolutionstuff.com/post/how-to-integrate-razorpay-payment-gateway-in-laravel)_**