PHP Arrays Tutorial

An array is a data structure that saves one or more same type of values in a single value. For instance, if you like to save 100 numbers, then instead of showing 100 variables, it’s simple to define an array of 100 lengths.

There are three different types of arrays and each array value is accessed using an ID, which is known as array index.

  • Numeric array − An array with a numeric index. It stores and accesses the values in linear form.
  • Associative array − An array with strings as index. This stores element values in the association with key values rather than in a strict linear index order.
  • Multidimensional array − An array containing one or more arrays and values is accessed using numerous indices.

PHP Built-in array functions are given in the function reference PHP Array Functions

<?php

$school= array("library", "classroom", "sportsroom","playground",);
echo "In our school  " . $school[0] . ", " . $school[1] . ", " . $school[12].  " and " . $school[3] . ".";

?>

In our school library, classroom, sportsroom and playground.

Please see above example showing only one variable ($school) but stored multiple values i.e. library, classroom, sportsroom, playground. This is the benefit of PHP Array.

PHP Arrays Tutorial


Indexed Arrays : Indexed arrays can store numbers, strings but their index will be represented by numbers only and index only auto starts from zero value.

<?php

$numbers = array( 1, 2, 3, 4, 5, 6, 7);
foreach( $numbers as $value )
{
echo "Value is $value";
}

?>

Associative Arrays : Associative arrays use named keys that you assign to them, its similar as indexed arrays in functionality but in terms of index it’s different.

<?php

$age= array("john" => 45, "raj" => 52, "peter" => 37);
echo "Age of john is ".  $age['john']  . "<br />";
echo "Age of raj is ".  $age['raj'] . "<br />";
echo "Age of peter is ".  $age['peter'] . "<br />";

?>

Age of John is 45
Age of Raj is 52
Age of Peter is 37


Multidimensional Arrays : In multidimensional array, you can create array under array means like sub array. It’s very interesting to use.

<?php

$marks = array(
"john" => array (
"english" => 50,
"maths" => 30,
"science" => 40
"history" => 39
),

"raj" => array (
"physics" => 30,
"maths" => 95,
"science" => 48
"history" => 39
),

"peter" => array (
"physics" => 31,
"maths" => 22,
"science" => 78
"history" => 85
)

"sunny" => array (
"physics" => 62,
"maths" => 75,
"history" => 80
)
);

echo "John marks in english : " ;
echo $marks['john']['english'] . "<br />";

echo "Raj marks in maths : ";
echo $marks['raj']['maths'] . "<br />";

echo "Peter marks in science : " ;
echo $marks['peter']['science'] . "<br />";

echo "Sunny marks in history : " ;
echo $marks['peter']['history'] . "<br />";

?>

John marks in english :  50
Raj marks in maths :  95
Peter marks in science :  78
Sunny marks in history :  80




Coding Tag

Coding Tag always happy to serve you in Information Technology (IT). Learn HTML, CSS, Javascript, PHP, Android, Digital Marketing, Latest IT News and many more.

Leave a Reply

avatar
  Subscribe  
Notify of