Defining Arrays

Arrays can be defined in PHP as a variable.

$fruits = array("Banana", "Orange", "Apple");

However, unlike other variables you can NOT use echo to output the content of the entire array. The syntax used to output the content of an array is print_r.For example:

print_r($fruits);
echo "<br>";
print_r($fruits[1]);


Array ( [0] => Banana [1] => Orange [2] => Apple )
Orange

However, you can use echo to output the content of items of the array

print_r($fruits);
echo "<br>";
echo ($fruits[1]);


Array ( [0] => Banana [1] => Orange [2] => Apple )
Orange

$grades = array(85.56, 76.00, 93.56); print_r($grades);
echo "<br>";
print_r($grades[1]);
echo "<br>";
echo ($grades[1]);


Array ( [0] => 85.56 [1] => 76 [2] => 93.56 )
76
76

Associative Arrays

PHP arrays are associative, therefore you can use anything as the index value, including but not limited to non-sequential values.

$name[3] = "Ethan";
print_r($name);


Array ( [3] => Ethan )

$user[favoriteColor] = "Green";
echo ("User's favorite color is ".$user[favoriteColor].".");


User's favorite color is Green.

Associaltive arrays can be defined as a variable as well. However, you have to put the index name is quotes and separate (assign) the value by using =>. For exampls:


$languages = array("france" => "French", "india" => "Hindi", "brazil" => "Portugese");
print_r($languages);
echo ("<br>");
echo ("The language spoken in Brazil is ".$languages[brazil].".");


Array ( [france] => French [india] => Hindi [brazil] => Portugese )
The language spoken in Brazil is Portugese.

Appending an element (value) to an Arrays

You can add a new element to an array by using the syntax [] and assigning a value to it. The new element is added at the end. For example:

$fruits[] = "Grapes";
print_r($fruits);


Array ( [0] => Banana [1] => Orange [2] => Apple [3] => Grapes )

Determining the size of an Arrays (i.e. determining the number of objects in an array)

You can determine the size of an array by using the syntax sizeof. For example:

$lengthFruits = sizeof($fruits);
echo ("The size of array "fruits" is ".$lengthFruits);


The size of array "fruits" is 4.

Deleting a variable, or an object/element/value of an array, or the array itself

You can delete a variable, inclusing an array, or a specific item/value/object of an array by using the syntax unset. For example:

$myName = "Pavan";
$allNames = ("Pavan", "Lori", "Ethan", "Asha");

echo ("My name is ...".$myName);
echo ("<br>");
echo ("All elements in arary "allNames" are... ");
print_r($allNames);
echo ("<br>");
echo ("The second name in the array is ...".$allNames[1]);

unset($myName);
echo ("<br>");
echo ("My name is ...".$myName).".";

unset($allNames[1]);
echo ("<br><br>");
echo ("The second name in the array is ...".$allNames[1]);

echo ("<br><br>");
echo ("All elements in array "allNames" are... ");
print_r($allNames);


My name is ...Pavan
All elements in array "allNames" are... Array ( [0] => Pavan [1] => Lori [2] => Ethan [3] => Asha )
The second name in the array is ...Lori
My name is ....

The second name in the array is ...

All elements in array "allNames" are... Array ( [0] => Pavan [2] => Ethan [3] => Asha )

NOTE: If you noticed, even after I deleted the second object in the array "allNames", i.e. the object at offset 1, the remaining indexes don't get renamed. They retain their original index names (thus making the PHP arrays associative).