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 :-
(i) $flower=array (“rose”, “marigold”, “jasmine”, “sunflower”, “orchid”, “tulip”);
(ii)
$flower[0]=”rose”;
$flower[1]=”marigold”;
$flower[2]=”jasmine”;
$flower[3]=”sunflower”;
$flower[4]=”orchid”;
$flower[5]=”tulip”;

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:-
(i) $salary=array(“A”=>”35000”, “John”=>”45000”, “C”=>”20000”);
(ii)

$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:
// Creation of a multidimensional array
$fruits = array(
array(“apple”, 5, “red”),
array(“banana”, 3, “yellow”),
array(“grape”, 4, “black”)
);

// Accessing or printing individual element from the multidimensional array
echo $fruits[0][0];      // Output: apple
echo $fruits[1][1];      // Output: 3
echo $fruits[2][2];     // Output: black

// Accessing or printing elements from the multidimensional array using Loop
foreach ($fruits as $value) {
  echo $value[0] . ” – “ . $value[1] . ” – “ . $value[2] . “<br>”;
}
// Output:
// apple – 5 – red
// banana – 3 – yellow
// orange – 4 – orange

// Add a new element to the existing multidimensional array
$fruits[] = array(“guava”, 2, “green”);

// Accessing or printing the newly added element
echo $fruits[3][0]; // Output: guava
Hybrid Array
    • 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:
$emp= array(
“name” => “John”,
“age” => 41,
“dept” => “Marketing”,
0 => “Male”,
    2 => 45000
);
echo$emp[2]; // Output: 45000
echo$emp[“age”]; // Output: 41
//prints all the values either numeric or string index
foreach ($emp as $key => $value) {
if (is_int($key))
{
echo “Element with numerical index $key is $value.<br>”;
}
else
{
echo “Element with string key ‘$key‘ is $value.<br>”;
}
}

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 –
      $flower= array (“Rose”, “Marigold”, “Jasmine”);
      sort($flower);



        Loading

        Categories: Php Theory

        0 Comments

        Leave a Reply

        Your email address will not be published. Required fields are marked *

        This site uses Akismet to reduce spam. Learn how your comment data is processed.