An array is a data structure collection object that contains multiple variables. The variables contained in an array are called elements of the array, and in PHP they can be accessed by their computed indexed indices or referenced named keys. PHP arrays are indexed using the zero index approach, meaning that the first element in the array has an index value of 0, the second element has an index value of 1, the third element has an index value of 2, and so on and so forth, and incrementing sequentially until the end of the array.
In PHP there are three types of arrays:
In PHP, array items (the object contained within an array element as a value) can be of any data type, such as strings, integers, floats, etc. Array items can also be objects, structs, classes, functions, and even other arrays.
In PHP, it is acceptable to have different data types in the same array. Other languages, such as C#, require that all data types of an array be of the same variable type, however PHP does not have this restriction. This means that in PHP you can create an array where the first element is a string, the second element is an integer, the third element is another array, the fourth element might be another string, etc... While PHP may allow this, it is not recommended practice since it can be difficult as a programmer for you to keep track of what's what within an array.
The syntax for indexed arrays:
array(value, value, value, etc...);
The following is an example of how to create an indexed array, and we are storing string values in the arrays elements:
$herosArray = array ("Superman" , "Wonder Woman" , "Batman" , "The Flash" );
The following is an example of how to create and access an indexed array, and we are storing string values in the arrays elements, and then accessing each element of the array by that elements index value within the echo statement:
<!DOCTYPE html >
<html >
<head >
</head >
<body >
<?php
$herosArray = array ("Superman" , "Wonder Woman" , "Batman" , "The Flash" );
echo "The best DC Heros are: " . $herosArray [0 ] . ", " . $herosArray [1 ] . ", " . $herosArray [2 ] . ", and " . $herosArray [3 ] . "." ;
? >
</body >
</html >
Output:
We can create an array of integers as follows:
<!DOCTYPE html >
<html >
<head >
</head >
<body >
<?php
$testScoresArray = array (85 , 95 , 90 , 80 );
echo "Hero test scores are: " . $testScoresArray [0 ] . ", " . $testScoresArray [1 ] . ", " . $testScoresArray [2 ] . ", and " . $testScoresArray [3 ] . "." ;
? >
</body >
</html >
Output:
We can also create an array of both strings and integers as follows:
<!DOCTYPE html >
<html >
<head >
</head >
<body >
<?php
$heroTestScoresArray = array ("Clark" , 85 , "Diana" , 95 , "Bruce" , 90 , "Barry" , 80 );
echo ( "Hero test scores are: " .
$testScoresArray [0 ] . " = " . $testScoresArray [1 ] . ", " .
$testScoresArray [2 ] . " = " . $testScoresArray [3 ] . ", " .
$testScoresArray [4 ] . " = " . $testScoresArray [5 ] . ", " .
$testScoresArray [6 ] . " = " . $testScoresArray [7 ] . ", " .
"." );
? >
</body >
</html >
Output:
The syntax for associative arrays:
array(key=>value, key=>value, key=>value, etc...);
The key can be a numeric or string value.
The following is an example of creating an associative array:
<!DOCTYPE html >
<html >
<head >
</head >
<body >
<?php
$heroTestScoresArray = array ("Clark" =>85 , "Diana" =>95 , "Bruce" =>90 , "Barry" =>80 );
echo "Wonder Woman got a score of " . $heroTestScoresArray ['Diana' ] . ", which is the highest score of everyone!" ;
? >
</body >
</html >
Output:
Note: It is important to understand that in an associative array the key value takes the place of the indexed value, therefore if you still want to use an indexed value in an associative array (for whatever your reasons are) then you would need to define it and set it like any other key value pair. The key value can be a number or a string. If it is a number it does not have to be the indexed count value for that array element either (although it will make it very difficult to remember what's what within the array):
<!DOCTYPE html >
<html >
<head >
</head >
<body >
<?php
$heroTestScoresArray = array ("Clark" =>85 , "Diana" =>95 , "Bruce" =>90 , "Barry" =>80 , 2 =>100 );
echo "The array element value for key value of 2 is " . $heroTestScoresArray [2];
? >
</body >
</html >
Output:
A Multidimensional array is an array of arrays. This means that the element value of the array is another array. This is often used for specifying an array of rows of columns to make grids and spreadsheets.
<?php
$superHeros = array (
array ("Superman" , 80, 90, 85),
array ("Wonder Woman" , 95, 100, 95),
array ("Batman" , 85, 95, 90),
array ("The Flash" , 80, 70, 75)
);
echo ($superHeros [2][0] . "'s scores are " . $superHeros [2][1] . ", " . $superHeros [2][2] . ", and " . $superHeros [2][3] . "." );
? >
Output:
One of the most common tasks performed on an array is to sort it. PHP offers a very simple function to accomplish this quick and easy:
The syntax for sort is:
sort(array, sorttype);
where array
is required, and sorttype
is optional. The available options for sorttype
are:
<!DOCTYPE html >
<html >
<head >
</head >
<body >
<?php
$herosArray = array ("Superman" , "Wonder Woman" , "Batman" , "The Flash" );
sort ($herosArray );
foreach ($herosArray as $key => $val ) {
echo "herosArray[" . $key . "] = " . $val . "<br>" ;
}
? >
</body >
</html >
Output:
There are times when you may need to add an element to an existing array, such as when building an array within a for loop.
The following is an example of adding an element to an existing indexed array:
<?php
// creating the indexed array
$herosArray = array ("Superman" , "Wonder Woman" , "Batman" , "The Flash" );
// adding a new element to the existing array
$herosArray = ["Aquaman" ];
? >
The following is an example of adding an element to an associative array:
<?php
// creating the associative array
$heroTestScoresArray = array ("Clark" =>85 , "Diana" =>95 , "Bruce" =>90 , "Barry" =>80 );
// adding a new element to the existing array
$heroTestScoresArray ["Aquaman" ] = "75" ;
? >
Another way to insert elements to the end of an array is by using the array_push()
function, which can be used in both indexed and associative arrays:
<?php
// creating the indexed array
$herosArray = array ("Superman" , "Wonder Woman" , "Batman" , "The Flash" );
// adding a new element to the existing array
array_push ($herosArray , "Aquaman" , "Green Lantern" , "Black Adam" );
? >
Note: Although you can use the array_push()
method to add new elements to the end of an existing associative array in the exact same way that you use it to add new elements to the end of an indexed array, it is advised to use caution when doing so. When using the array_push()
method in an associative array, the array_push()
method will automatically assign a unique numerical value as that new elements key value, even if all of the existing elements in the associative array have string key values. This can result in making it very difficult for you to find and retrieve the elements value from its key value if you don't know what that key value is.
Thank you for reading, I hope you found this blog post (tutorial) educational and helpful.
About | Contact Us | Privacy | Terms & Conditions | © 2024 - T&J Divisions, LLC, All Rights Reserved |