Hello folks! welcome back to a new section of our tutorial on PHP. In this section of our PHP tutorial, we are going to be discussing about Arrays in PHP.
Arrays in PHP is a data structure that allows us store one or more similar type of values in a single value. For example if you want to store 200 numbers then instead of defining 200 variables it's easy to define an array of 200 length.
There are three different kind of arrays and each of the array value is accessed with an ID c which is called the array index.
Arrays in PHP is a data structure that allows us store one or more similar type of values in a single value. For example if you want to store 200 numbers then instead of defining 200 variables it's easy to define an array of 200 length.
There are three different kind of arrays and each of the array value is accessed with an ID c which is called the array index.
- Numeric array - An array with numeric index. Values are stored and accessed in a linear mode.
- Associative array - Associative array is an array with string as index. This stores element values in association with key values rather than in a strict linear index order.
- Multidimensional array - It is an array containing one or more arrays. Values are accessed using multiple indices.
READ: Loops in PHP
Numeric Array
These arrays can be used to store numbers, strings and any object but their index will be represented by numbers. Array index begins from zero by default.
Example
The following is an example showing how to create and access numeric arrays.
Here, we have used the array() function to create an array.
Here, we have used the array() function to create an array.
<html> <body> <?php /* First method to create array. */ $numbers = array( 1, 2, 3, 4, 5); foreach( $numbers as $value ) { echo "Value is $value <br />"; } /* Second method to create array. */ $numbers[0] = "one"; $numbers[1] = "two"; $numbers[2] = "three"; $numbers[3] = "four"; $numbers[4] = "five"; foreach( $numbers as $value ) { echo "Value is $value <br />"; } ?> </body> </html>
Output
When the above example is executed, it will produce the following result -
Value is 1 Value is 2 Value is 3 Value is 4 Value is 5 Value is one Value is two Value is three Value is four Value is five
READ: PHP Decision Making
Associative Array
Associative arrays are identical to numeric arrays in terms of functionality but they are different in terms of their index. Associative arrays will have their index as string so you can establish a strong association between keys and values.
To store the employees salaries in an array, a numerically indexed array wouldn't be the best choice. rather, we could use the names of the employees as keys in our associative array and the value would be their individual salary.
Note - Do not keep associative arrays inside double quote while printing your result, else it would not return any value back.
To store the employees salaries in an array, a numerically indexed array wouldn't be the best choice. rather, we could use the names of the employees as keys in our associative array and the value would be their individual salary.
Note - Do not keep associative arrays inside double quote while printing your result, else it would not return any value back.
Example
Try the following example below -
<html> <body> <?php /* First method to associate create array. */ $salaries = array("prince" => 2000, "martins" => 1000, "precious" => 500); echo "Salary of prince is ". $salaries['prince'] . "<br />"; echo "Salary of martins is ". $salaries['martins']. "<br />"; echo "Salary of precious is ". $salaries['precious']. "<br />"; /* Second method to create array. */ $salaries['prince'] = "high"; $salaries['martins'] = "medium"; $salaries['precious'] = "low"; echo "Salary of prince is ". $salaries['prince'] . "<br />"; echo "Salary of martins is ". $salaries['martins']. "<br />"; echo "Salary of precious is ". $salaries['precious']. "<br />"; ?> </body> </html>
Output
When the above example is executed, it will produce the following result -
Salary of prince is 2000 Salary of martins is 1000 Salary of precious is 500 Salary of prince is high Salary of martins is medium Salary of precious is low
READ: PHP Comparison Operators
Multidimensional Array
In multi-dimensional array, each element in the main array can be an array as well. And each of the element in the sub-array can be an array, etc. Values in a multi-dimensional array are accessed using multiple index.
Example
In the following example below we create a two dimensional array to store the marks of three students in three subjects -
The below example is an associative array, you can create a numeric array is the same way.
The below example is an associative array, you can create a numeric array is the same way.
<html> <body> <?php $marks = array( "prince" => array ( "physics" => 35, "maths" => 30, "chemistry" => 39 ), "martins" => array ( "physics" => 30, "maths" => 32, "chemistry" => 29 ), "precious" => array ( "physics" => 31, "maths" => 22, "chemistry" => 39 ) ); /* Accessing multi-dimensional array values */ echo "Marks for prince in physics : " ; echo $marks['prince']['physics'] . "<br />"; echo "Marks for martins in maths : "; echo $marks['martins']['maths'] . "<br />"; echo "Marks for precious in chemistry : " ; echo $marks['precious']['chemistry'] . "<br />"; ?> </body> </html>
Output
When the above example is executed, it will produce the following result -
Marks for prince in physics : 35 Marks for martins in maths : 32 Marks for precious in chemistry : 39
READ: PHP Constants
Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP Strings.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.