Introduction of Array in Php
- It’s one of the most popular, simple, linear and static data structures.
Definition of Array in Php
- PHP array is a storage that holds multiple values of similar type in a single variable.
Features of Array in Php
- An array holds a group of values.
- We can store either number, string or object type of data in the Php array.
- We can easily traverse array in Php using foreach loop.
- It is used to store data in contiguous memory location.
- It is mostly used to implement other data structures.
Type of Array in Php
- There are 3 types of array in Php. These are –
1. Indexed/Numeric Array
2. Associative Array
3. Multidimensional Array
4. Hybrid/Mix Array
Indexed/Numeric Array
-
- Indexed array in Php is an array where all the stored values are represented by an index number by default started from 0.
- Indexed array can store numbers, strings or any object values.
- There are two ways to define indexed array :-
$flower[1]=”marigold”;
$flower[2]=”jasmine”;
$flower[3]=”sunflower”;
Associative Array
-
- In this array, we can associate a unique name/label with each array elements using => symbol.
- We can easily remember/call the elements of this array because each element is represented by an easy label or name.
- We can easily traverse the elements of Php associative array using specific For Each loop.
- There are two ways to define associative array:-
$salary[“A”]=>”35000″;
$salary[“John”]=>”45000″;
$salary[“C”]=>”20000″;
Multidimensional Array
-
- A multidimensional array is an array that contains one or more arrays as its elements.
- Each element in a multidimensional array can itself be an array, creating a nested structure of arrays.
- For Example:
-
- A hybrid array is a type of Php array that combines the features of both indexed arrays and associative arrays.
- A hybrid array can contain both numerical and string keys. It allows us to access elements of the array using either a numerical index or a string key.
- For example:
In-built Function in Php Array
- array() :
- Php array() function is used to create and returns an array.
- It allows to create indexed, associative and multidimensional arrays.
- For example: –
$flower= array (“Rose”, “Marigold”, “Jasmine”);
- array_chunk() :
- Php array_chunk() function splits/divides array into multiple chunks/parts.
- The syntax is – (array_chunk($array_name, chunk_value)).
- array_reverse() :
- Php array_reverse() function returns the array contents in reversed order of its original.
- The syntax is – (array_reverse($array_name)).
- array_search() :
- Php array_search() function searches the specified value in an array.
- It returns key value as index of array if search is successful otherwise nothing returns.
- The syntax is – (array_search($array_name)).
- count() :
- Php has count() function which returns length of an array.
- The syntax is – (count($array_name)).
- is_array() :
- An in-built function is_array() is used to test whether a value is an array.
- The syntax is – (is_array($array_name)).
- sort() :
- We can sort the array elements using sort functions.
- For example –
0 Comments