When creating long, complex programs, it sometimes becomes difficult to access
certain elements in a program. for example there is a limitation for using
variables in a program: when you declare a variable, you can assign only one
value to it. In certain case , you may want to declare hundreds of variables and
it may not be possible to remember each and every variable and use it. Array
help to accomplish this. An array is a special kind of variable that unlike
conventional variables, can store multiple values that are indexed. To
understand this definition let it split it into three parts:.
- An array is a special kind of variable because it is primarily use for strong
values.
- Unlike conventional variables, array can store multiple values without any
limitation.
- Each value that is assigned to an array is accessed using an index in the form
of a number or a string.
There are a few underlying features of array that help qualify them as one of
the most important elements in any complex program.
-
Any number of values can be stored in a single array.
-
The value stored in an array can be numeric or string.
-
After the value are stored in the array, they can be extracted from it at any
given time.
-
Arrays enhance the scope and flexibility of a program by allowing data to be
accessed easily.
-
Array organize complex datastructure that are related in some way.
-
It is possible to add values to an array after it is created. which is not possible
in case of a variable.
-
Values in an array can be retrieved sequentially and randomly.
Creating an array is as simple as creating a variable. The only difference is
that its main purpose is to hold multiple related values in the same
place. consider the fallowing example
<?php $week[] = "Sunday"; $week[] = "Monday"; $week[] = "Tuesday"; /* And So on */ ?>
<?php $week = array("Sunday", "Monday", "Tuesday","etc.."); ?>
Arrays that are indexed by using strings are called associative arrays. When you
use them, you specified a string instead of number within the square brackets.
<?php $student[Name] = "Rishi"; $student[Age] = "8"; $student[Game] = "Chess"; ?>OR
<?php $student = array(Name=>"Rishi", Age=>"8", Game=>"Chess"); ?>
As discuss earlier, array can store numbers as well as names. They can also store
other arrays. Such arrays are multidimensional arrays or an array of arrays.
<?php $student = array( array( Name=>"Rishi", Age=>"8", Game=>"Chess"), array( Name=>"Sambit", Age=>"9", Game=>"Chess"), array( Name=>"Rupal", Age=>"10", Game=>"Chess"), ); echo $student[1][Age]; ?> Output: 9;
Another method of accessing the elements in an array and displaying them is by
looping. You can use the foreach statement to loop through enumerated,
associative and multidimensional arrays.
As discuss earlier, the foreach statement comes in handy when looping through an
enumerated array. Consider the fallowing example
<?php foreach ($array as $val){ echo "My Value=".$val ."<br/>"; } ?>
The foreach statement is also used for looping through an associative arrays, but
there is a slight change in the way you use it. You would need to specified the
key as well as the value of an element to access a particular element. consider
an example
<?php foreach ($array as $key =>$val){ echo " $key = $val <br/>"; } ?>
After learning how to loop through enumerated arrays and associative arrays, you
might want to know how to loop through a multidimensional arrays. You can combine
your knowledge to looping through enumerated and associative arrays to
accomplish this.
<?php $student = array( array( Name=>"Rishi", Age=>"8", Game=>"Chess"), array( Name=>"Sambit", Age=>"9", Game=>"Chess"), array( Name=>"Rupal", Age=>"10", Game=>"Chess"), ); foreach($student as $st){ foreach($st as $key => $val){ echo "$key = $val"."<br/>"; } echo "<hr>"; } ?> Output: Name = Rishi Age = 8 Game = Chess ------------------------------------- Name = Sambit Age = 9 Game = Chess -------------------------------------- Name = Rupal Age = 10 Game = Chess