Wednesday, September 23, 2020

A Guide to PHP Arrays



Hello dear readers! welcome back to another section of my tutorial on PHP. In this section of our PHP tutorial, we will be studying about Arrays in PHP.

An array is a data structure that stores one or more similar types of values in a single value. For example if you want to store 20 numbers, instead of defining 20 variables its very easy to define an array of 20 length.

There are three different kind of arrays available in PHP. Each of the array value is accessed with an ID c which is called the array index.

  • Numeric array - It is an array with numeric index. Values are stored and accessed in a linear mode.
  • Associative array - An array with string as an index. This reserves element values in association with key values rather than in a strict linear index order.
  • Multidimensional array - An array carrying one or more arrays. Values are accessed using multiple indices.

RECOMMENDED: Loops in PHP


Numeric Array

This PHP array can store numbers, strings, and any object but their index is going to be represented by numbers. By default an array index starts from zero.

Example

Following below is an example showing how to create and also access numeric arrays.

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 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 

RECOMMENDED: PHP Decision Making


Associative Array

The associative arrays are very similar to the numeric arrays in terms of functionality but they are so different in terms of their index. The associative array will have their index as string so you can set up a strong association between keys and values.

To store employees salaries in an array, a numerically indexed array wont be the best choice. Instead, we could use the employees names as keys in our associative array, and the value would be their respective salary.

Note - Do not keep associative arrays in double quote while you are printing, else it would not a single 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 code 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

RECOMMENDED POST: PHP Comparison Operators


Multidimensional Array

In a multidimensional array, each element in the main array can be an array. And then each element in the sub-array can be an array, etc. Values in multidimensional array are are accessed using multiple index.

Example

In this example we create a two dimensional array to store the marks of three students in three subjects -

This 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 code 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

RECOMMENDED: PHP Constants

Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial post, we are going to be discussing about the PHP Strings.

Feel free to ask your questions where necessary and i 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.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.