PHP Array Functions With Example

Websolutionstuff | Apr-23-2021 | Categories : Laravel PHP

In this tutorial we will learn 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 start to learn php array function with example.

Create an Array in PHP

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

 

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 = ['Websolutionstuff', 'Web', 'Solution', 'Stuff'];

 

 

//add to array
$names[] = 'Websolutionstuff';

 

Array merge :

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

 

Array to string :

// Array to string
$text = implode(', ', $names); // 'Websolutionstuff, Web, Solution'

 

String to Array :

// String to Array
echo explode(',', $text); // ['Websolutionstuff', 'Web', 'Solution']

 

Remove and preserve keys :

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

 

 

Remove and change keys :

// Or remove and change keys
array_splice($names, 1, 1);
// It is now => [0 => 'Websolutionstuff', 1 => 'Solution']

 

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 exist :

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

 

Array filter :

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

 

Search all value :

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

 

Recommended Post
Featured Post
Laravel 8 Group By Query Example
Laravel 8 Group By Query Examp...

In this example we will see laravel 8 group by query example. how to use group by in laravel 8. As you might expect...

Read More

Nov-29-2021

Paginate Method Example in Laravel 8
Paginate Method Example in Lar...

In this post i will share you information about paginate method example in laravel 8. As we know laravel provide many...

Read More

Jun-28-2021

How To Restrict User Access From IP Address In Laravel 9
How To Restrict User Access Fr...

Imagine this: You've made a super cool website, and now you want to make sure only the right people can use it. That...

Read More

Jan-03-2023

Laravel 10 Custom Validation Error Message
Laravel 10 Custom Validation E...

In this article, we will see the laravel 10 custom validation error message. Here, we will learn about how to creat...

Read More

May-19-2023