An array is a special type of variable that can hold many values at once, all accessible via a single variable name. Arrays are very useful whenever you need to work with large amounts of data — such as records from a database — or group related data together. Arrays in PHP
How arrays works
- An array can hold any number of values, including no values at all.
- Each value in an array is called an element.
- You access each element via its index, which is a numeric or string value. Every element in an array has its own unique index.
- An element can store any type of value, such as an integer, a string, or a Boolean. You can mix types within an array — for example, the first element can contain an integer, the second can contain a string, and so on.
- An array’s length is the number of elements in the array.
- An array element’s value can itself be an array. This allows you to create multidimensional arrays.
How to create an array in PHP
It’s easy to create an array within a PHP script. To create an array, you use the array() construct:
$arrayName=array(values);
Types of PHP Arrays
- Indexed Array: Arrays with sequential numeric index, such as 0,1,2 etc.
To create an indexed array, just list the array values inside the parentheses, separated by commas. The following example creates an indexed array of color names and stores it in a variable called $color:<?php $colors=array("red","orange","green","blue","pink","yellow"); ?>
When creating an indexed array, PHP automatically assigns a numeric index to each element. In the above example, “red” is given an index of 0, “orange” has an index of 1, and so on.
- Associative Array: This is the most frequently used type of PHP Arrays whose elements are defined in key/value pairs.
To create an associative array, you pair each value with the index that you want to use for that value. To do this you use the => operatorindex => value or <?php $variable_name['key_name'] = value; $variable_name = array('keyname' => value); ?>
The following example creates an associative array with information about a student, and stores it in a variable called $student:
<?php $stuent = array( "name" => "Alfred Hitchcock", "rollno" => 101, "class" => "First Year" ); ?>
- Multidimensional Array: Arrays whose elements may contain one or more arrays. There is no limit in the level of dimensions.
A multidimensional array is also known as an array of arrays.
Example:<?php $emp = array ( array(1,"sonoo",400000), array(2,"john",500000), array(3,"rahul",300000) ); ?>
By the way, to create an array with no elements, you can write:
$arrayName = array();
Accessing array elements using for and foreach loop:
foreach
The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes:
foreach (array_expression as $value) statement foreach (array_expression as $key => $value) statement
The first form loops over the array given by array_expression. On each iteration, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next iteration, you’ll be looking at the next element).
The second form will additionally assign the current element’s key to the $key variable on each iteration.
- Indexed Array
<?php $arr = array(1, 2, 3, 4); foreach ($arr as &$value) { echo $value; } ?>
- Associative Array
<?php $name = [ 'firstname' => 'John', 'lastname' => 'Doe', 'middlename' => 'Bray' ]; foreach ($name as $key => $value) { echo $key . ':' . $value . '<br>'; }
- Multidimensional Array
<?php $emp = array ( array(1,"sonoo",400000), array(2,"john",500000), array(3,"rahul",300000)); for ($row = 0; $row < 3; $row++) { for ($col = 0; $col < 3; $col++) { echo $emp[$row][$col]." "; } echo "<br/>"; } ?>