This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Smartphone icons created by Freepik - Flaticon
4.1.1 Indexed Arrays
- There are two ways to create indexed arrays:
- The index can be assigned automatically (index always starts at 0), like this:
$cars = array("Volvo", "BMW", "Toyota");
or the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
The following example creates an indexed array named $cars, assigns three elements to it, and then prints a text containing the array values:
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Change Value
To change the value of an array item, use the index number:
To change the value of an array item, use the index number:
$cars = array("Volvo", "BMW", "Toyota");
$cars[1] = "Ford";
var_dump($cars);
Loop Through an Indexed Array
To loop through and print all the values of an indexed array, you could use a for loop, like this:
<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>
Index Number
- The key of an indexed array is a number, by default the first item is 0 and the second is 1 etc., but there are exceptions.
- New items get the next index number, meaning one higher than the highest existing index.
- So if you have an array like this:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
And if you use the array_push() function to add a new item, the new item will get the index 3:
array_push($cars, "Ford");
var_dump($cars);
But if you have an array with random index numbers, like this:
$cars[15] = "Volvo";
$cars[7] = "BMW";
$cars[14] = "Toyota";
And if you use the array_push() function to add a new item, what will be the index number of the new item?
array_push($cars, "Ford");
var_dump($cars);
Complete PHP Array Reference
Animated PHP icons used in the buttons provided by ICONS8.COM.
Smartphone icons created by Freepik - Flaticon
Module 4 quiz
Example files created in this module:
PHP Arrays
Array Items
PHP Arrays - Get The Length of an Array with The count() Function
PHP Arrays - Change Value
Indexed Arrays - Loop Through an Indexed Array
PHP Arrays: Index Number part 1
PHP Arrays: Index Number part 2
PHP Arrays - Associative Arrays part 1
PHP Arrays - Associative Arrays part 2
PHP Arrays - Access Associative Arrays
PHP Associative Arrays: Change Key's Value
Associative Arrays - Loop Through an Associative Array part 1
Associative Arrays - Loop Through an Associative Array part 2
PHP Arrays - Two-dimensional Arrays
PHP Arrays - Two-dimensional Arrays with for loop
PHP Arrays - Sorting Arrays part 1
PHP Arrays - Sorting Arrays part 2
PHP Arrays - Sort Array in Descending Order rsort() part 1
PHP Arrays - Sort Array in Descending Order rsort() part 2
PHP Arrays - Sort Array (Ascending Order), According to Value asort()
PHP Arrays - Sort Array (Ascending Order), According to Key ksort()
PHP Arrays - Sort Array (Descending Order), According to Value arsort()
PHP Arrays - Sort Array (Descending Order), According to Key krsort()
PHP $GLOBALS
PHP $_SERVER
PHP $_REQUEST - Form Submission
PHP $_POST - Form Submission
Test Get
PHP RegEx - Using preg_match()
PHP RegEx - Using preg_match_all()
PHP RegEx - Using preg_replace()
PHP RegEx - Grouping