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]);
However, you can use echo to output the content of items of the array
print_r($fruits);
echo "<br>";
echo ($fruits[1]);
$grades = array(85.56, 76.00, 93.56);
print_r($grades);
echo "<br>";
print_r($grades[1]);
echo "<br>";
echo ($grades[1]);
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);
$user[favoriteColor] = "Green";
echo ("User's favorite color is ".$user[favoriteColor].".");
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].".");
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);
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);
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);
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).