One-Dimensional Array
The array that contains only one dimension and each array element is stored in a particular index is called a one-dimensional array. It can be numeric or associative.
Different examples of one-dimensional arrays are shown in the following section:
Example-1: Use of one-dimensional numeric array
The one-dimensional numeric array can be defined in various ways in PHP. In the following script, $arr1 array contains string values that are declared by specifying the index values. Next, $arr2 array contains numeric values that is declared by using array() function. Lastly, a $arr3 array contains mixed data that is also declared by using the array() function.
//Create index-based array of string by specifying the index value
$arr1[0] = "Good";
$arr1[1] = "Better";
$arr1[2] = "Best";
//Access the element based on index
echo "<h3>The second index value of arr1 is <font color="blue">$arr1[1]</font></h3>";
//Create index-based array of numbers with array values
$arr2 = array(10, 55, 34, 89, 20);
//Access the element based on index
echo "<h3>The third index value of arr2 is <font color="green">$arr2[2]</font></h3>";
//Create index-based array of different data with array values
$arr3 = array("Hello",10, TRUE, 67.5, '15-10-20');
//Read numeric array using for loop
echo "<h3>The values of arr3 are:</h3>";
foreach ($arr3 as $value){
echo "$value<br />";
}
?>
Output:
The following output will appear after running the script. The value of the second index of $arr1 is printed in the first output. The value of the third index of $arr2 is printed in the second output. All values of $arr3 are printed using for loop in the third output.
Example-2: Use of the one-dimensional associative array
A one-dimensional associative array can be declared as the one-dimensional numeric array. The following script shows the declaration of three types of one-dimensional associative array like the previous example.
//Create an associative array by specifying each index separately
$arr1["1110"] = "Abir Hossain";
$arr1["7845"] = "Kabir Hossain";
$arr1["2390"] = "Nurjahan Akter";
//Access the element based on index
echo "<h3>The first index value of arr1 is <font color="blue">".$arr1['1110']."</font></h3>";
//Create an associative array by using array() function
$arr2 = array("CSE201"=>"Data Structure and Algorithm","CSE303"=>"Networking",
"CSE401"=>"Multimedia", "CSE202"=>"Object Oriented Programming");
//Read the keys and values of associative array using for loop
echo "<h3>The values of associative array, arr2 are:</h3>";
foreach ($arr2 as $key => $value){
echo "Course ID: <b>".$key."</b>, Course Name: <b>".$value."</b> <br />";
}
?>
Output:
The following output will appear after running the script. The particular index value of $arr1 and all keys and values of $arr2 are printed using the foreach loop.
Two-dimensional Array
When the array contains two indexes to store the elements, then the array is called a two-dimensional array. A two-dimensional array is used to store tabular data, which contains a fixed number of rows and columns. How two-dimensional array can be declared and used in PHP are shown in the next part of this tutorial.
Example-3: Use of the two-dimensional numeric array
A two-dimensional numeric array is declared in the following script. The first index of the array will contain the row values, and the second index of the array will contain the column values. According to the script, $websites array contains three rows and three columns. The first row contains the list of three search engine websites. The second row contains a list of three e-commerce websites. The third row contains three money transfer websites. Two for loops are used to identify the index of row and column values of the array and print the array values.
//Create a two-dimensional numeric array
$websites = array(
array("google.com", "ask.com", "bing.com"),
array("aliexpress.com", "ebay.com", "amazon.com"),
array("paypal.com", "payoneer.com", "skrill.com")
);
// Access the paricular element of the two-dimension array
echo $websites[0][2]." is a search engine site<br />";
echo $websites[1][1]." is a e-commerce site.<br />";
echo $websites[2][0]." is an online money transfer site.<br />";
//Read numeric array using for loop
for($i=0; $i<3; $i++)
{
if($i == 0)
echo "<h3>The list of search engine websites are:</h3>";
elseif($i == 1)
echo "<h3>The list of e-commerce websites are:</h3>";
else
echo "<h3>The list of online money transfer websites are:</h3>";
for($j=0; $j < 3; $j++){
echo $websites[$i][$j]."<br />";
}
}
?>
Output:
The following output will appear after running the above script.
Example-4: Use of the two-dimensional associative array
The following script shows the use of a two-dimensional associative array where the index values of both row and column are string. Here, the first array() function defines the index value of the row that contains the name of the teacher, and the two columns are defined by another array() function in each row. The index of each column contains the course code, and the value of each column contains the course name. The keys and values of the array will print by using the foreach loop.
"CSE105" => "C Programming"),
"Nehal Ahmed"=>array("CSE103" => "Physics-I",
"CSE203" => "Physics-II")
);
//Read two-dimensional associative array using for loop
foreach ($teachers as $teacher => $courses){
echo "<h2>Teacher Name: $teacher</h2>";
echo "<b>Course List:</b><br />";
foreach ($courses as $id => $course){
echo $course."<br />";
}
}
?>
Output:
The following output will appear after running the above script.
Conclusion:
The use of different types of the array is explained in this tutorial by using simple examples. Many built-in functions exist in PHP that are meant to do different types of tasks with array variables that are not explained in this tutorial. The ways of defining and working with one-dimensional and two-dimensional array variables are shown in this tutorial to help the new PHP user to know the purpose of using array and apply it in their PHP script.