PHP Array
- An array is a collection of similar data elements stored at contiguous memory locations
- An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more.
- An array is a special variable, which can hold more than one value at a time.
<?php
$cityName = array('Delhi', 'Mumbai', 'Kolkata');
echo 'I like ' . $cars[0] . ', ' . $cars[1] . ' and ' . $cars[2] ;
?>
output:
Array
(
[0] => ra
[1] => one
[2] => game
)
In PHP, there are three types of arrays:
- Indexed arrays - Arrays with a numeric index.
- Associative arrays - Arrays with named keys.
- Multidimensional arrays - Arrays containing one or more arrays.
PHP Indexed Arrays
There are two ways to create indexed arrays:
<?php
// manually create index.
$arr[0] = 'ra';
$arr[1] = 'one';
$arr[2] = 'game';
//to looking out put of array then use print_r() or var_dump() function.
//if required formatted output then use
print_r($arr);
?>
output
Array
(
[0] => ra
[1] => one
[2] => game
)
PHP Associative Arrays
<?php
$age = array('Ram'=>35, 'Shyam'=>37, 'Ravi'=>43);
print_r($age);
echo 'Ram age is: '.$age['Ram'];
?>
output
Array ( [Ram] => 35 [Shyam] => 37 [Ravi] => 43 )
Ram age is: 35
PHP Multidimensional Arrays
A multidimensional array is an array containing one or more arrays.
<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
print_r($cars);
var_dump($cars);
?>
output
Array
(
[0] => Array
(
[0] => Volvo
[1] => 22
[2] => 18
)
[1] => Array
(
[0] => BMW
[1] => 15
[2] => 13
)
[2] => Array
(
[0] => Saab
[1] => 5
[2] => 2
)
[3] => Array
(
[0] => Land Rover
[1] => 17
[2] => 15
)
)
array(4) {
[0]=>
array(3) {
[0]=>
string(5) "Volvo"
[1]=>
int(22)
[2]=>
int(18)
}
[1]=>
array(3) {
[0]=>
string(3) "BMW"
[1]=>
int(15)
[2]=>
int(13)
}
[2]=>
array(3) {
[0]=>
string(4) "Saab"
[1]=>
int(5)
[2]=>
int(2)
}
[3]=>
array(3) {
[0]=>
string(10) "Land Rover"
[1]=>
int(17)
[2]=>
int(15)
}
}
Related post
EknowledgePoint
Increase Your Knowledge with usEknowledgePoint is an innovative online learning platform that aims to democratize education by providing a vast array of courses and educational content to learners of all ages and backgrounds. It offers a diverse range of subjects, from academic topics to practical skills, making it a one-stop destination for anyone looking to expand their knowledge base.
5 comments
Knowledge is the fuel that powers our journey through life. It's platforms like EknowledgePoint that transform information into enlightenment, curiosity into competence, and dreams into reality. Embrace the opportunity to learn, and you'll find that the doors of possibility swing wide open. 🚀💡 #EknowledgePoint.
Thank you for your kind words! At EknowledgePoint, we are dedicated to empowering individuals on their learning journey. Your enthusiasm for knowledge and personal growth inspires us to keep providing the best educational resources and experiences. Let's continue to explore and learn together!.
Thanks For Your Support.
"I'm constantly amazed by the quality of instructors on EknowledgePoint. Learning from experts in their fields is both enlightening and empowering. 🌟👨🏫 #EknowledgePoint.
EknowledgePoint isn't just a platform; it's a community of learners. The support and camaraderie here are truly inspiring. 🤝🌍 #EknowledgePoint.