PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
Arrays in PHP are a type of data structure that allows you to store multiple values in a single variable. An array can hold many values, and the values can be of any PHP type like numbers, strings, or even other arrays.
Creating Arrays
You can create an array in PHP using the array()
function, or with the short array syntax []
:
// Using array() $fruits = array("Apple", "Banana", "Cherry"); // Using [] $fruits = ["Apple", "Banana", "Cherry"];
Accessing Array Elements
You can access the values in an array by referring to the index number:
echo $fruits[1]; // Outputs: Banana
Array indices in PHP start from 0, so $fruits[1]
refers to the second element in the array.
Associative Arrays
Associative arrays are arrays that use named keys instead of numeric keys. You can create them like this:
$ages = array("John" => 35, "Jane" => 32, "Bob" => 29); // or $ages = ["John" => 35, "Jane" => 32, "Bob" => 29];
In this example, the names are the keys, and the ages are the values. You can access the values by referring to the keys:
echo $ages["John"]; // Outputs: 35
Looping Through Arrays
You can use a foreach
loop to iterate over an array:
foreach ($fruits as $fruit) { echo $fruit . "\n"; }
This will print each fruit on a new line. If you're working with an associative array and you want to access both the keys and the values, you can do it like this:
foreach ($ages as $name => $age) { echo "$name is $age years old.\n"; }
Array Functions
PHP provides a lot of functions to work with arrays, like count()
to get the number of elements, sort()
to sort the elements, array_push()
to add an element, and many more. For example:
$fruits = ["Apple", "Banana", "Cherry"]; echo count($fruits); // Outputs: 3 sort($fruits); print_r($fruits); // Outputs: Array ( [0] => Apple [1] => Banana [2] => Cherry ) array_push($fruits, "Durian"); print_r($fruits); // Outputs: Array ( [0] => Apple [1] => Banana [2] => Cherry [3] => Durian )
That's a quick introduction to arrays in PHP. They're a fundamental part of the language, and you'll use them a lot when you're writing PHP code.
PHP associative arrays:
$person = [ 'name' => 'John', 'age' => 30, 'city' => 'New York', ];
PHP indexed arrays:
$colors = ['Red', 'Green', 'Blue'];
PHP multidimensional arrays:
$matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ];
PHP array functions:
count
, array_push
, array_pop
, array_merge
, etc.$numbers = [1, 2, 3, 4, 5]; $count = count($numbers);
PHP array initialization:
$fruits = ['Apple', 'Banana', 'Orange'];
PHP array manipulation:
array_push
, array_pop
, array_shift
, array_unshift
, etc.$colors = ['Red', 'Green']; array_push($colors, 'Blue');
PHP array sorting:
sort
, rsort
, asort
, ksort
, etc.$numbers = [4, 2, 8, 1, 6]; sort($numbers);
PHP array merging:
array_merge
function.$arr1 = ['a', 'b']; $arr2 = ['c', 'd']; $merged = array_merge($arr1, $arr2);
PHP array iteration:
foreach
.$fruits = ['Apple', 'Banana', 'Orange']; foreach ($fruits as $fruit) { echo $fruit . "\n"; }