PHP Arrays (original) (raw)

Arrays are one of the most important data structures in PHP. They allow you to store multiple values in a single variable. PHP arrays can hold values of different types, such as strings, numbers, or even other arrays. Understanding how to use arrays in PHP is important for working with data efficiently.

Types of Arrays in PHP

There are three main types of arrays in PHP:

**1. Indexed Arrays

Indexed arrays use numeric indexes starting from 0. These arrays are ideal when you need to store a list of items where the order matters.

**Now, let us understand with the help of the example:

PHP `

`

You can also explicitly define numeric keys in an indexed array:

PHP `

"apple", 1 => "banana", 2 => "cherry"); ?>

`

**2. Associative Arrays

Associative arrays use named keys, which are useful when you want to store data with meaningful identifiers instead of numeric indexes.

**Now, let us understand with the help of the example:

PHP `

"GFG", "age" => 30, "city" => "New York"); echo $person["name"]; ?>

`

**3. Multidimensional Arrays

Multidimensional arrays are arrays that contain other arrays as elements. These are used to represent more complex data structures, such as matrices or tables.

**Now, let us understand with the help of the example:

PHP `

array("age" => 25, "grade" => "A"), "GFG" => array("age" => 22, "grade" => "B") ); echo $students["GFG"]["age"]; ?>

`

Creating Array in PHP

In PHP, arrays can be created using two main methods:

**1. Using the array() function

The traditional way of creating an array is using the array() function.

$fruits = array("apple", "banana", "cherry");

**2. Using short array syntax ([])

In PHP 5.4 and later, you can use the shorthand [] syntax to create arrays.

$fruits = ["apple", "banana", "cherry"];

You can also create associative arrays by specifying custom keys:

$person = ["name" => "GFG", "age" => 30];

**Note: Both methods are valid, but the shorthand syntax is preferred for its simplicity and readability.

Accessing and Modifying Array Elements

1. Accessing Array Elements

You can access individual elements in an array using their index (for indexed arrays) or key (for associative arrays).

$fruits = ["apple", "banana", "cherry"]; echo $fruits[0]; // Outputs: apple

$person = ["name" => "GFG", "age" => 30]; echo $person["name"]; // Outputs: GFG

2. Modifying Array Elements

You can modify an existing element by assigning a new value to a specific index or key.

$fruits = ["Apple", "Banana", "Cherry"]; $fruits[1] = "Mango"; // Changes "Banana" to "Mango" echo $fruits[1]; // Outputs: Mango

$person = ["name" => "GFG", "age" => 25]; $person["age"] = 26; // Updates the age to 26 echo $person["age"]; // Outputs: 26

Adding and Removing Array Items

1. Adding Array Elements

You can add new elements to an array using the following methods:

$fruits = ["apple", "banana"]; array_push($fruits, "cherry"); // Adds "cherry" to the end

array_unshift($fruits, "pear"); // Adds "pear" to the beginning

$person["city"] = "New York";

2. Removing Array Elements

To remove items from an array, you can use several functions:

array_pop($fruits);

array_shift($fruits);

unset($fruits[2]); // Removes the element with index 2

Array Functions

PHP provides a wide range of built-in functions to work with arrays. Here are some common array functions:

$array1 = [1, 2, 3]; array2=[4,5,6];<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi><mi>e</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>d</mi><mo>=</mo><mi>a</mi><mi>r</mi><mi>r</mi><mi>a</mi><msub><mi>y</mi><mi>m</mi></msub><mi>e</mi><mi>r</mi><mi>g</mi><mi>e</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">merged=arraymerge(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal">m</span><spanclass="mordmathnormal"style="margin−right:0.02778em;">er</span><spanclass="mordmathnormal"style="margin−right:0.03588em;">g</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">d</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal"style="margin−right:0.02778em;">rr</span><spanclass="mordmathnormal">a</span><spanclass="mord"><spanclass="mordmathnormal"style="margin−right:0.03588em;">y</span><spanclass="msupsub"><spanclass="vlist−tvlist−t2"><spanclass="vlist−r"><spanclass="vlist"style="height:0.1514em;"><spanstyle="top:−2.55em;margin−left:−0.0359em;margin−right:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingreset−size6size3mtight"><spanclass="mordmathnormalmtight">m</span></span></span></span><spanclass="vlist−s">​</span></span><spanclass="vlist−r"><spanclass="vlist"style="height:0.15em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal"style="margin−right:0.02778em;">er</span><spanclass="mordmathnormal"style="margin−right:0.03588em;">g</span><spanclass="mordmathnormal">e</span><spanclass="mopen">(</span></span></span></span>array1,array2 = [4, 5, 6]; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi><mi>e</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>d</mi><mo>=</mo><mi>a</mi><mi>r</mi><mi>r</mi><mi>a</mi><msub><mi>y</mi><mi>m</mi></msub><mi>e</mi><mi>r</mi><mi>g</mi><mi>e</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">merged = array_merge(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">m</span><span class="mord mathnormal" style="margin-right:0.02778em;">er</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">e</span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">rr</span><span class="mord mathnormal">a</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:-0.0359em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">m</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.02778em;">er</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">e</span><span class="mopen">(</span></span></span></span>array1, array2=[4,5,6];<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi><mi>e</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>d</mi><mo>=</mo><mi>a</mi><mi>r</mi><mi>r</mi><mi>a</mi><msub><mi>y</mi><mi>m</mi></msub><mi>e</mi><mi>r</mi><mi>g</mi><mi>e</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/xtex">merged=arraymerge(</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;verticalalign:0.1944em;"></span><spanclass="mordmathnormal">m</span><spanclass="mordmathnormal"style="marginright:0.02778em;">er</span><spanclass="mordmathnormal"style="marginright:0.03588em;">g</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">d</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="marginright:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;verticalalign:0.25em;"></span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal"style="marginright:0.02778em;">rr</span><spanclass="mordmathnormal">a</span><spanclass="mord"><spanclass="mordmathnormal"style="marginright:0.03588em;">y</span><spanclass="msupsub"><spanclass="vlisttvlistt2"><spanclass="vlistr"><spanclass="vlist"style="height:0.1514em;"><spanstyle="top:2.55em;marginleft:0.0359em;marginright:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingresetsize6size3mtight"><spanclass="mordmathnormalmtight">m</span></span></span></span><spanclass="vlists"></span></span><spanclass="vlistr"><spanclass="vlist"style="height:0.15em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal"style="marginright:0.02778em;">er</span><spanclass="mordmathnormal"style="marginright:0.03588em;">g</span><spanclass="mordmathnormal">e</span><spanclass="mopen">(</span></span></span></span>array1,array2); print_r($merged); // Outputs: [1, 2, 3, 4, 5, 6]

$fruits = ["Apple", "Banana", "Cherry"]; if (in_array("Banana", $fruits)) { echo "Banana is in the array!"; }

$numbers = [3, 1, 4, 1, 5]; sort($numbers); print_r($numbers); // Outputs: [1, 1, 3, 4, 5]

To read about the PHP Array Functions read this article – PHP Array Function

Array Iteration

You can loop through arrays using loops such as foreach or for.

$fruits = ["Apple", "Banana", "Cherry"]; foreach ($fruits as $fruit) { echo $fruit . "
"; }

$numbers = [1, 2, 3, 4]; for ($i = 0; i<count(i < count(i<count(numbers); $i++) { echo numbers[numbers[numbers[i] . "
"; }

To read about the PHP Array Iteration read this article – PHP Array Iteration