PHP Array
PHP Tutorial » PHP Arrays
What is an Array?
An array is a special variable that can hold more values at the some time.
In PHP, there are three types of arrays:
- Indexed or Numeric arrays - Matrix that contains a numeric index
- Associative arrays - Array containing named keys
- Multidimensional arrays - Arrays that containing one or more arrays
Create an Array in PHP
array() function is used to create an array:
Syntax:
Indexed or Numeric array
To loop through array and display all the values of an Indexed (Numeric) array, we use a "for" loop, as this:
<?php
$my_cars = array("Peugeot", "BMW", "Volvo", "Toyota", "Renault");
$arr_length = count($my_cars);
for($a = 0; $a < $arr_length; $a++) {
echo $my_cars[$a];
echo "<br>";
}
?>
Related subjects:
What is an array
Convert array to string comma
Array to comma separated
Tags: php array: push, length, append, function, merge, add, to string, size, foreach, key, key value, of arrays, filter
What are PHP arrays?
How do you create array in PHP?
What are the three types of arrays in PHP?
What is array key PHP?
PHP Associative Arrays
Associative arrays are more o less the same as numeric arrays in term of functionality but they are different in terms of their index
Associative arrays are arrays that use named keys that you assign to them.
We show you an example: Loop Through an Associative Array
<?php
$user_age = array("Chris"=>"32", "Benony"=>"27",
"Johny"=>"23", "Ana-Marie"=>"29", "Maru"=>"43");
foreach($user_age as $a => $a_value) {
echo "Age for " . $a . ", is =" . $a_value;
echo "<br>";
}
?>
PHP Multidimensional Arrays
A multi-dimensional array is an array containing one or more arrays.
<?php
$carsstsold = array (
array("Peugeot",24,14),
array("Renault",18,11),
array("BMW",9,2),
array("Land Rover",17,12),
array("Volvo",16,7)
);
echo $carsstsold[0][0].": - In stock: ".$carsstsold[0][1].", sold: ".$carsstsold[0][2].".<br>";
echo $carsstsold[1][0].": - In stock: ".$carsstsold[1][1].", sold: ".$carsstsold[1][2].".<br>";
echo $carsstsold[2][0].": - In stock: ".$carsstsold[2][1].", sold: ".$carsstsold[2][2].".<br>";
echo $carsstsold[3][0].": - In stock: ".$carsstsold[3][1].", sold: ".$carsstsold[3][2].".<br>";
echo $carsstsold[4][0].": - In stock: ".$carsstsold[4][1].", sold: ".$carsstsold[4][2].".<br>";
?>
Tags: php array: push, length, append, function, merge, add, to string, size, foreach, key, key value, of arrays, filter
What are PHP arrays?
How do you create array in PHP?
What are the three types of arrays in PHP?
What is array key PHP?
PHP Array - What is an Array? - php tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.