PHP Tutorial on Arrays with Stone River eLearning

We have talked about PHP variables in one of our previous blog posts and surely by this time you might have realized the importance of variables in PHP. In fact, being the storing blocks of values in a code, variables are essential and necessary in any programming language. We already know that a variable can be used to store data or values, but what if we need to store multiple values? Let say, I need something that can store the names of all the students in a class. Since the number of students will be around 30-40, assigning these many variables and then calling each one of them separately every time will be a tedious task to do, even on the computer. So what’s the better approach? Before we take a dip in the world of array, it is important to understand that array and variables are not substitutes of each other, but different storage containers which are used depending upon the task in hand. In simple words, an array is a special kind of variable that can store multiple numbers of values.

Eg:

<?php
 $fruits=array("Apple","Banana","Cherry");
 echo "I like " . $fruits[0] . ", " . $fruits[1] . " and " . $fruits[2] . "."; 
?>

It is important to note that while the name of an array is declared in the same way as in case of variables, the values are assigned inside closed parenthesis, in a comma separated format with each value included inside quotes. The keyword ‘array’ is critical in declaring an array as it informs PHP that $fruits is an array and not a simple variable.

Now you must be thinking,”Alright, the array declaration part is pretty clear, but what is this $fruits [0], $fruits [1] and $fruits [2]? I dint give numbers in the array, I gave strings.” Well, you are right, you gave strings but each value in the array is stored at a particular location and the number 0, 1, 2 etc (which is called index of an array) gives the location of that value in the array variable. So, if I need to select the 27th guy in an array variable of 60 students, I do not have to get all the first 27 students, instead I can simply write student [26] and get the 27th student in the array.
“Wait, wait, you said 27th student and you wrote student [26]? I am sure you made an error there.” Well, actually no, array stores value starting from index value 0, followed by 1, 2, 3 and so on and therefore the 27th guy will have an index value 26.

 

Note:

  1. The values inside an array must belong to the same data type. Therefore, you can have all integer values or all string values in an array, but you cannot have an array with values from both integer and strings.
  2. An array is a continuous block and it assigns memory as continuous blocks as well. This means that if value $fruits [0] is assigned memory location 2200, then $fruits [1] will be assigned the memory exactly next to this one.

 

Basically, there are three types of array:

  1. Indexed Arrays
  2. Associative Arrays
  3. Multidimensional Arrays
    We just discussed about the indexed arrays in the first example where each value in the array is assigned an index value which is then later used to refer to that array element.

 

Associative Array:

From our knowledge of arrays, it’s easy to understand that any array needs some token (index in case of indexed arrays) to be assigned to them so that these tokens can later be used to select a particular value from the array. Obviously, associative array needs a token assigned to it as well, however what makes it different from indexed array is the way and the kind of tokens assigned.

An associative array named or string tokens for the assignment and there are two ways to create an associative array:

Eg:

  1. $fruits=array(“Apple”=>”35″,”Banana”=>”20″,”Cherry”=>”60”);
    echo “The cost of Apple is .$fruits[‘Apple’ ].”;
  2. $fruits[‘Apple’]=”35″;
    $fruits[‘Banana’]=”20″;
    $fruits[‘Cherry’]=”60″;
    We will discuss about multi-dimensional array in the upcoming blog post.

GET YOUR FREE PHP EBOOK!

(Visited 54 times, 1 visits today)

Comments