Master php arrays in 10 minutes with source code

Friday, 12th July 2019

Array in php

Think of an array as a special variable that is capable of storing as many values as you passed.

Example

<?php

$x = array(array('x', 'y', 'z');

echo $x[0]; // x

echo $x[1]; //y

echo $x[2]; //z

?>

Assuming x, y, and z are all variables that could have been accessed differently by adding it all to one array it will be accessed using one variable $x.

So php arrays stores multiple variables in one single variable.

Arrays come in handy in php, let's say for instance that you have a list of items (list of programming languages you want to study), you can easily declare the variable like thus and therefore access each of it using its index.

Example

<?php

$myProgrammningLanguages = array('javascript','php','python','java','Ruby','Go');

echo $myProgrammningLanguages[0]; //javascript

echo $myProgrammningLanguages[1]; //php

echo $myProgrammningLanguages[2]; //python

echo $myProgrammningLanguages[3]; //java

echo $myProgrammningLanguages[4]; //Ruby

echo $myProgrammningLanguages[5]; //Go

?>

Looking at the above example you will understand that even if you have up to 100 programming language that it will be very easy to access and as well you will keep track at the order at which you have prioritized any of them.

But if you are to be declaring the variables one after the order you will observe that it will be hard work and accessing each of the items orders will become a headache without to you.

How to create an array in php

In php, there are two methods of creating an array.

  1. The php array() function can be used to create an array

Example:

<?php 

$arrayName = array('english','mathematics','computer','science','art');

// the above is the array of subjects studied in the college

?>

This type of array is created by passing in the required list as a string into the php array() function.

  1. Using the square brackets [];

You can also create a php array using the square brackets. Take a look at the example below to understand it very well

Example

<?php

  $arrayName = ['english','mathematics','computer','science','art'];

 echo $arrayName[0]; // english  echo $arrayName[1]; // mathematics

 ?>

The above example is also perfect for creating arrays in php. I often use the last method though each of them is perfectly correct.

Types of arrays in php

In php, there are three types arrays which you will often come across as you progress.

  1. Indexed arrays: Arrays with a numeric index.

  2. Associative arrays: Arrays with named keys.

  3. Multi-dimensional arrays:   Arrays containing more than one array.

PHP Indexed Arrays

By default, all arrays created without keys just as the ones we have been creating before are all indexed arrays. ( Indexed arrays, therefore, starts at 0 indexes). The first item in the array has the 0 indexes and it counts to the last item in the array.

example

<?php
$myProgrammningLanguages = array('javascript','php','python','java','Ruby','Go');
echo $myProgrammningLanguages[0]; //javascript
echo $myProgrammningLanguages[1]; //php
echo $myProgrammningLanguages[2]; //python
echo $myProgrammningLanguages[3]; //java
echo $myProgrammningLanguages[4]; //Ruby
echo $myProgrammningLanguages[5]; //Go
?>

You can as well create the same type of array above using the below method

<?php

$myProgrammningLanguages[0] = 'javascript';
$myProgrammningLanguages[1] = 'php';
$myProgrammningLanguages[2] = 'python';
$myProgrammningLanguages[3] = 'java';
$myProgrammningLanguages[4] = 'Ruby';
$myProgrammningLanguages[5] = 'Go';
?>

What this means is that you are passing in the array's index to be assigned to each programming language and php automatically loop through it and creates an array for you. Whereby you can do anything you do in other arrays with it.

Get the total number of items in the PHP array.

To get the total number of items in an array or the length of an array you use any of the following function

<?php

$myProgrammningLanguages = array('javascript','php','python','java','Ruby','Go');
echo count($myProgrammningLanguages)  
// this takes in the name of the array as its parameter
echo Sizeof($myProgrammningLanguages) 
// this takes in the name of the array as a parameter

/* *
the above will output the same thing 
and as well you can use any of the function
*/.

?>

Loop through an indexed array.

Looping through an indexed array is very easy in php. The main thing you need to know to achieve this thing perfectly is any  of the following

  • for loop

  • foreach loop

read more on php for loop function and php foreach loop function

having known that let us get started

<?php

$myProgrammningLanguages = array('javascript','php','python','java','Ruby','Go');

$size = sizeof($myProgrammningLanguages);


foreach ($myProgrammningLanguages as $language) {

        echo $language.'<br />';

}

echo "<hr />";


for ($i=0; $i < $size; $i++) { 

        echo($myProgrammningLanguages[$i].'<br>');

}

?>

The two functions give you the same thing. However, I often go with the first example because it uses less syntax nevertheless, the second method is also good.

PHP Associative arrays

Associative arrays are arrays that use named keys and values. In this case, you provide a key for accessing each array and no longer use the default number index that php uses to access the array.

You can easily create associative arrays in php using any of the two methods of creating an array.

Example

<?php

$myArray = array('Nigeria'=>'Abuja', 'Europe'=>'New York');

$myArray1 = ['Nigeria'=>'Abuja', 'Europe'=>'New York'];

?> 

//The two arrays are pectly the same thing and as well lets loop through it

<?php

$myArray = array('Nigeria'=>'Abuja', 'Europe'=>'New York');

$myArray1 = ['Nigeria'=>'Abuja', 'Europe'=>'New York'];

foreach ($myArray as $key => $value) {

        echo($key. ' '.'='.' '.$value.'<br />');

}

//try this with the variable $myArray1 and see if you will get the same result.

?>

And that it is for the associative arrays.

You access it by using it in key-value method and you will loop through it and print it tp the screen.

Multi-dimensional array

Php multi-dimensional array is an array that has other arrays in it. For instance, you will come across this type of arrays in often when you are working with database result and that has foreign keys. All the methods performed on an index and associative arrays are exactly what you will use to work on a multi-dimensional array.

Thank you so much for reading if you have understood this tutorial please share it with your friends on social media and leave a comment so that I can help you if you are having any problem in php

Leave a Reply

RELATED POSTS


Total of 0 Comment