• Have Any Question?
  • contact@eitworld.com

PHP Arrays





PHP Arrays is the group of similar data elements having the contiguous memory location.In array, first element represents the lowest address and last element represents the highest address and element of array is uniquely identified via index number that starts with 0. PHP Arrays is of several types such as Associative Array, Indexed Array, Multi-dimensional Array. For example, if you want to make the cart for shopping sites in PHP then you have to use Associative Array in PHP which accepts the values using key and value.

At eitworld.com you can learn PHP Arrays in simple way with lots of explained examples.

  • Arrays are the variables which are used to store multiple values.

Need of array

  • What is the need of array while you can create as many individual variables as you want?
  • Suppose if you have 500 values to store for example you want to save a record of students in a school. It will be irritating and almost impossible to create 500 different variables for each detail of the students. Here comes the need of array. You can create a single array and save all the 500 values in it. There are very less variables to handle when we use array.
  • If you want to search for a particular value from many variables, this is not possible while individual variables are there. But in array, you can easily loop through the array to access all the values in it, find your desired value and manipulate it according to your need.

Syntax of Array

    • In php array() function is used to create an array.

$arr= array("value1","value2","value2");

This is the basic form of creating an array. $arr is becomes an array which will contained the values that are written inside array() function. count($arr) returns the length of the array which can be used to loop through the array.

Types of array

  • There are three types of array in PHP:
    • Indexed Array- These arrays have numeric index.
    • Associative Array- These arrays have named index.
    • Multidimensional Array- These arrays themselves have more than one array.

Indexed Array

    • Indexed array are created in following way-

$arr= array("value1","value2","value2");
    • In above array, the index will be started from 0. The index for value1 will be 0, for value2 will be 1, for value3 will be 2 and so on.
    • You may also assign values individually using index-

$arr[0]= "value1";
$arr[1]= "value2";
$arr[2]= "value3";

Looping

    • Use a for loop to go through entire array and use its counter as the index of array to access the values.

Example-


<!DOCTYPE html>
<html>
<body>
<?php
$bowlers= array("Johnson","Malinga","Lee");
$length=count($bowlers);
for($a=0;$a<length;$a++)
{
echo $bowlers[$a];
echo "<br>";
}
?>
</body>
</html>

Output

  • Above example creates an array variable $bowlers. The length of the array is obtained in $length. Now we are accessing the values using for loop. The for loop is run till the length of array and the values are accessed one by one in each loop.

Associative Array

    • Associative arrays can be created in following way-

$arr= array("index1"=>"value1","index2"=>"value2","index3"=>"value3");
    • Here both indexes and values must be specified in array() function.
    • You may also assign values individually using index.

$arr["index1"]= "value1";
$arr["index2"]= "value2";
$arr["index3"]= "value3";

Looping

    • Looping in associative arrays is done in following manner-

foreach($arr as $index=>$values)
{ }
    • Now $index will be representing the indexes of the array and $values will represent the values at respective indexes. See the example-

<!DOCTYPE html>
<html>
<body>
<?php
$bowlers= array("Johnson"=>"6","Malinga"=>"5","Lee"=>"4");
foreach($bowlers as $name=>$wicket)
{
echo "For Index= ".$name.", Value= ".$wicket;
echo "<br>";
}
?>
</body>
</html>

Multidimensional Array

  • An array which contains more than one array is called Multidimensional Array.
  • Two dimensional array, Three Dimensional array, Four Dimensional array etc are the types of Multidimensional Array. In this tutorial we will learn the two dimensional form of multidimensional array because arrays more than two dimensions are typical to manage.

Two Dimensional Array

    • An array of arrays is known as Two Dimensional Array.
    • Here is the basic syntax of two dimensional array which contains name and age of two persons:

$person = array
(
array("Ram",30),
array("Sohan",22)
);


Real Time Web Analytics