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.
- PHP offers a wide range of built-in array functions for sorting, merging, searching, and more.
- PHP Arrays can store values of different types (e.g., strings, integers, objects, or even other arrays) in the same array.
- They are dynamically sized.
- They allow you to store multiple values in a single variable, making it easier to manage related data.
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).
- **Accessing Indexed Array:
$fruits = ["apple", "banana", "cherry"]; echo $fruits[0]; // Outputs: apple
- **Accessing Associative Array:
$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.
- **Modifying Indexed Array Element:
$fruits = ["Apple", "Banana", "Cherry"]; $fruits[1] = "Mango"; // Changes "Banana" to "Mango" echo $fruits[1]; // Outputs: Mango
- **Modifying Associative Array Element:
$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:
- **array_push(): Adds elements to the end of an indexed array.
$fruits = ["apple", "banana"]; array_push($fruits, "cherry"); // Adds "cherry" to the end
- **array_unshift(): Adds elements to the beginning of an indexed array.
array_unshift($fruits, "pear"); // Adds "pear" to the beginning
- **Direct assignment: Adds an element to an associative array.
$person["city"] = "New York";
2. Removing Array Elements
To remove items from an array, you can use several functions:
- **array_pop(): Removes the last element from an indexed array.
array_pop($fruits);
- **array_shift(): Removes the first element from an indexed array.
array_shift($fruits);
- **unset(): Removes a specific element from an array by key or index.
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:
- **Array Merge: The array_merge() function combines two or more arrays into one.
$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="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); print_r($merged); // Outputs: [1, 2, 3, 4, 5, 6]
- **Array Search: The in_array() function checks if a specific value exists in an array.
$fruits = ["Apple", "Banana", "Cherry"]; if (in_array("Banana", $fruits)) { echo "Banana is in the array!"; }
- **Array Sort: The sort() function sorts an indexed array in ascending order.
$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.
- **Using foreach Loop:
$fruits = ["Apple", "Banana", "Cherry"];
foreach ($fruits as $fruit) {
echo $fruit . "
";
}
- **Using for Loop:
$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