Dreevoo.com | Online Learning and Knowledge Sharing
Home | Programs | Programming languages | PHP & MySQL | Arrays in PHP
Click to view your profile

Arrays in PHP

Arrays in PHP allows us to store more values into one variable which makes access to the values much easier.

  Author: podtalje | Version: php.net | 3rd November 2013 |  

The most basic way to store values in PHP is to use variables with different names.

$car1 = "Saab";
$car2 = "Volvo";
$car3 = "BMW";

You can find out more about variables in


In PHP and also in other programming languages there is also a more efficient way to store values in arrays.

To define an array we set name of a variable and add an index.
Example with variable $cars:

$cars[0] = "Saab";
$cars[1] = "Volvo";
$cars[2] = "BMW";
$cars[3] = "Toyota";

echo $cars[0] . ", " . $cars[1];

In the example above we defined an array and used echo to print out the values.


Short way for defining array is to use keyword array.

Instead of indexes we can also use names which can be seen in the example below.

$age = array("Peter" => 32, "John" => "57", "Sarah" => 23);

echo "Sarah is " . $age["Sarah"] . " years old.";


Here we have another way to define the array which is a little longer, but the end result is exactly the same as in the previous step.

$age["Peter"] = 32;
$age["John"] = 57;
$age["Sarah"] = 23;

echo "John is " . $age["John"] . " years old.";


We can also define multidimensional array which you can see in the example below.

$person["Peter"]["age"] = 32;
$person["Peter"]["nickname"] = "Pete";
$person["Peter"]["hair"] = "Brown";

$person["John"]["age"] = 57;
$person["John"]["nickname"] = "Johnny";
$person["John"]["hair"] = "Black";

echo "John is " . $person["John"]["age"] . " years old.";


When we are working with arrays we can also use print_r function which will output all the indexes and values of the array.    

$age["Peter"] = 32;
$age["John"] = 57;
$age["Sarah"] = 23;



One of the main advantages of using arrays is that you can easily access all the values by using foreach loop.

$age["Peter"] = 32;
$age["John"] = 57;
$age["Sarah"] = 23;

foreach($age as $key=>$value) {
  echo "Value of $key is $value. <br />";   

By using foreach we go through all the keys in the array and print out the values.

If you want to know more about using loops in PHP please see

  Please login to post a comment
  Click to open user profile  
thefansbuzz, 19th Feb 2023, 7:32 PM
Cheap SMM Panel Social Media Marketing is a huge field with a lot of competition. If you want to stand out from the crowd, you need to know how to find ways to make your content go viral. If you are interested in using social media to market your product, this service is perfect for you. The panel gives you access to hundreds of different social media sites, so that you can find new ways to market your business. Visit our website at: https://thefansbuzz.com/
  Click to open user profile  
Juliett78, 20th Aug 2023, 3:50 PM
Hi guys,The code example demonstrates the convenience of using arrays in PHP, particularly with foreach loops. This approach simplifies access to values within an associative array, as shown with the $age array. nickname
online learning made for people