Array in PHP
Arrays in PHP
An array is a data structure that can hold multiple values in a single variable. Arrays are widely used in PHP to store collections of data, such as lists, tables, or multiple values associated with a single key.
Types of Arrays in PHP
Indexed Arrays (also called Numeric Arrays)
Associative Arrays
Multidimensional Arrays
1. Indexed Arrays
Indexed arrays are arrays where the index (key) is automatically assigned by PHP, starting from 0.
Example:
<?php$fruits = array("Apple", "Banana", "Cherry");echo $fruits[0]; // Outputs: Appleecho $fruits[1]; // Outputs: Bananaecho $fruits[2]; // Outputs: Cherry?>Adding Elements:
$fruits[] = "Orange"; // Adds 'Orange' to the end of the array2. Associative Arrays
Associative arrays use named keys (instead of numbers) to access the values.
Example:
<?php$person = array( "name" => "John", "age" => 25, "city" => "New York");echo $person["name"]; // Outputs: Johnecho $person["age"]; // Outputs: 25echo $person["city"]; // Outputs: New York?>Adding Elements:
$person["email"] = "john@example.com"; // Adds a new key-value pair3. Multidimensional Arrays
A multidimensional array is an array that contains one or more arrays. It can be thought of as an array of arrays.
Example:
<?php$people = array( array("name" => "John", "age" => 25), array("name" => "Jane", "age" => 28), array("name" => "Doe", "age" => 22));echo $people[0]["name"]; // Outputs: Johnecho $people[1]["age"]; // Outputs: 28?>Array Functions in PHP
PHP provides a wide range of functions to manipulate arrays:
Array Length (count())
$fruits = array("Apple", "Banana", "Cherry");echo count($fruits); // Outputs: 3Array Sorting (sort())
Sorts the array in ascending order:
$fruits = array("Banana", "Apple", "Cherry");sort($fruits);print_r($fruits); // Outputs: Array ( [0] => Apple [1] => Banana [2] => Cherry )Array Merging (array_merge())
Combines two or more arrays into one:
$array1 = array("Apple", "Banana");$array2 = array("Orange", "Grapes");$merged = array_merge($array1, $array2);print_r($merged); // Outputs: Array ( [0] => Apple [1] => Banana [2] => Orange [3] => Grapes )Array Search (in_array())
Checks if a value exists in an array:
$fruits = array("Apple", "Banana", "Cherry");if (in_array("Banana", $fruits)) { echo "Banana is in the array!";} else { echo "Banana is not in the array.";}Array Keys (array_keys())
Returns all the keys in an array:
$person = array("name" => "John", "age" => 25);$keys = array_keys($person);print_r($keys); // Outputs: Array ( [0] => name [1] => age )Nested Loops to Traverse Arrays
If you have multidimensional arrays, you can loop through them with a nested loop:
<?php$people = array( array("name" => "John", "age" => 25), array("name" => "Jane", "age" => 28), array("name" => "Doe", "age" => 22));foreach ($people as $person) { echo "Name: " . $person["name"] . ", Age: " . $person["age"] . "<br>";}?>Conclusion
Arrays are a fundamental part of PHP and are essential for storing and managing collections of data. You can perform various operations on arrays using built-in PHP functions, and depending on your need, you can choose between indexed, associative, or multidimensional arrays.
Would you like to explore specific array operations or advanced array features in PHP?