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

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 |  
 
 
1.
 

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


 
 
2.
 

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.

 
 
3.
 

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.";

 
 
4.
 

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.";

 
 
5.
 

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.";

 
 
6.
 

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;

print_r($age);

 
 
7.
 

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
 
 
  Click to open user profile  
helendam, 10th Jun 2024, 4:45 AM
Ahoy, esteemed adventurer! On this glorious day, I wanted to https://heardledecades.io extend my heartfelt greetings, infused with the spirit of exploration, limitless possibilities, and the unwavering belief in your ability to conquer new horizons.
 
 
  Click to open user profile  
oucanshall, 10th Jun 2024, 10:52 AM
Geometry Dash Free has easy-to-learn controls but compelling gameplay. You have to use the spacebar or click the mouse to guide a geometric shape—a cube, a ball, or a UFO, for example—as it navigates gaps, spikes, and other hazards. https://geometrydashwave.com
 
 
  Click to open user profile  
elliotzucker, 14th Aug 2024, 9:03 AM
The control panel is very accessible and performs https://fisheatfish.io in the best way possible.
 
 
  Click to open user profile  
Vanessa, 5th Nov 2024, 10:57 AM
PHP all values is very practical to help me, storage as variables remains the most basic method, they as an indispensable part.https://www.bestrealdoll.com/
 
 
  Click to open user profile  
Vanessa, 5th Nov 2024, 10:57 AM
PHP all values is very practical to help me, storage as variables remains the most basic method, they as an indispensable part.https://www.bestrealdoll.com/
 
 
  Click to open user profile  
Vanessa, 5th Nov 2024, 10:58 AM
PHP all values is very practical to help me, storage as variables remains the most basic method, they as an indispensable part.https://www.bestrealdoll.com/
 
 
  Click to open user profile  
dundunali, 23rd Apr 2025, 12:07 PM
The Ultimate Guide to Online Gaming Platforms in 2025
The online gaming landscape has evolved dramatically in recent years, offering players a diverse ecosystem of platforms that cater to different gaming preferences. From immersive casino experiences to skill-based card games and fantasy sports, today's digital gaming world has something for everyone. Let's explore some of the leading platforms that are making waves in the online gaming community.
Premium Gaming Experiences
B9 Game has established itself as a leader in immersive gaming platforms. Offering a diverse selection of exciting games, rewarding experiences, and thrilling challenges, it's become a go-to destination for many gaming enthusiasts. You can access their platform at https://b9ga.me/ or download their application directly from https://b9ga.me/download/. They also maintain a presence at https://b9game.space/ with downloads available at https://b9game.space/download/.
Bolly Game provides an unparalleled gaming experience to players worldwide. With a wide range of games spanning classic favorites and modern options, players can always find something they enjoy. Daily rewards, weekly promotions, and a VIP loyalty program ensure players feel valued. Their ecosystem includes:
Their live casino section brings the thrill of real-time gaming to your screen, allowing you to interact with professional dealers and other players while enjoying classic table games from the comfort of your home. For those seeking extra value, check out their promotions section at https://bollygame.cc/promotions/, packed with fantastic bonus opportunities including free spins, free cash, birthday bonuses, referral rewards, and cashback offers.
Action-Packed Gaming
K1 Game delivers an electrifying gaming experience with seamless gameplay, instant payouts, and action-packed tournaments. Whether you love strategy or fast-paced action games, there's something for everyone. Their platform can be accessed through multiple domains including:
Specialized Gaming Platforms
The ecosystem extends to specialized platforms catering to specific gaming interests:
11 Wickets offers an exciting fantasy sports experience with seamless gameplay at https://11wicketsapp.com/.
66 Rummy brings the ultimate rummy gaming experience where players can win big at https://66rummy.cc/ or https://66rummys.com/.
AA Game offers an immersive gaming experience with a wide range of games at https://aa-game.net/, while variants like AA Game 777 (https://aagame-777.com/) and AA Game Casino (https://aagame.casino/) focus on online casino gaming with exciting slots and table games.
Betting Platforms
For those interested in online betting, several platforms offer exciting opportunities:
Banzai Bet delivers a thrilling online betting experience with fast-paced gameplay and quick payouts at https://banzai-bet.fun/ or https://banzaibetfun.com/.
Bet O Bet offers an exciting world of online betting with seamless gameplay at https://betobetgame.com/.
Golden Cakra provides seamless online betting on live cricket, football, and casino games at https://golden-cakra.com/.
MostBet brings the excitement of online betting with big rewards at https://most-bet-game.com/.
Raj Bet offers thrilling betting options and big rewards at https://rajbet.cc/.
Unique Gaming Experiences
The landscape also features unique gaming experiences:
Crash Game offers a high-octane gaming experience with fast-paced gameplay and big rewards at https://crashgame.xyz/.
Dreamplay1 brings an exciting world of seamless gameplay and thrilling tournaments at https://dreamplay1.xyz/ or https://dreamplay1game.com/.
EK GAME offers an exhilarating gaming experience at https://ek-game.com/ or https://ekgame.xyz/.
Fomototo is your ultimate destination for a seamless gaming experience with fast payouts at https://fomototo.net/.
Rslot provides the thrill of online slots with exciting games and big rewards at https://rslot.xyz/.
Card Game Specialists
For card game enthusiasts, particularly rummy lovers:
Rummy Game offers seamless gameplay and thrilling tournaments at https://rummy-game.net/.
Rummy 100 provides the ultimate rummy experience with fast gameplay and instant rewards at https://rummy100.com/.
Rummy SE delivers excitement with seamless gameplay and instant payouts at https://rummyse.online/.
Getting Started
The online gaming world continues to evolve with new platforms emerging regularly. Whether you're looking for casino experiences, skill-based games, or betting opportunities, these platforms offer diverse options for entertainment. Always remember to game responsibly and check the legal status of online gaming in your jurisdiction before participating.
For newcomers to Y1 Game, you can get started at https://y1gamdownload.com/ to begin your gaming adventure.
As the digital entertainment landscape continues to evolve, these platforms represent the cutting edge of what's available to gaming enthusiasts in 2025, offering unprecedented variety and accessibility to players around the world.
 
   
 
 
online learning made for people
Dreevoo.com | CONTRIBUTE | FORUM | INFO