MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Complete reference guide of PHP Arrays!

What is PHP Arrays

Arrays are complex variables that allow us to store more than one value or a group of values under a single variable name. Let’s suppose you want to store colors in your PHP script. Storing the colors one by one in a variable could look something like this:

<?php 
$color1 = "Red";
$color2 = "Green";
$color3 = "Blue"; 
?> Code language: HTML, XML (xml)

But what, if you want to store the states or city names of a country in variables and this time this not just three may be hundred. It is quite hard, boring, and bad idea to store each city name in a separate variable. And here array comes into play.

Types of Arrays in PHP

There are three types of arrays that you can create. These are:

  • Indexed array â€” An array with a numeric key.
  • Associative array â€” An array where each key has its own specific value.
  • Multidimensional array â€” An array containing one or more arrays within itself.

Indexed Arrays

An indexed or numeric array stores each array element with a numeric index. The following examples shows two ways of creating an indexed array, the easiest way is:

<?php 
// Define an indexed array 
$colors = array("Red", "Green", "Blue"); 
?> Code language: HTML, XML (xml)

Note: In an indexed or numeric array, the indexes are automatically assigned and start with 0, and the values can be any data type.

Associative Arrays

In an associative array, the keys assigned to values can be arbitrary and user defined strings. In the following example the array uses keys instead of index numbers:

<?php 
// Define an associative array 
$ages = array("Peter"=>22, "Clark"=>32, "John"=>28); 

?>Code language: HTML, XML (xml)

Multidimensional Arrays

The multidimensional array is an array in which each element can also be an array and each element in the sub-array can be an array or further contain array within itself and so on. An example of a multidimensional array will look something like this:

<?php
// Define a multidimensional array
$contacts = array(
    array(
        "name" => "Peter Parker",
        "email" => "peterparker@mail.com",
    ),
    array(
        "name" => "Clark Kent",
        "email" => "clarkkent@mail.com",
    ),
    array(
        "name" => "Harry Potter",
        "email" => "harrypotter@mail.com",
    )
);
// Access nested value
echo "Peter Parker's Email-id is: " . $contacts[0]["email"];
?>Code language: HTML, XML (xml)

Viewing Array Structure and Values

You can see the structure and values of any array by using one of two statements — var_dump() or print_r(). The print_r() statement, however, gives somewhat less information. Consider the following example:

<?php
// Define array
$cities = array("London", "Paris", "New York");
 
// Display the cities array
print_r($cities);
?>Code language: HTML, XML (xml)

The print_r() statement gives the following output:
Array ( [0] => London [1] => Paris [2] => New York )
This output shows the key and the value for each element in the array. To get more information, use the following statement:

<?php
// Define array
$cities = array("London", "Paris", "New York");
 
// Display the cities array
var_dump($cities);
?>Code language: HTML, XML (xml)

This var_dump() statement gives the following output:
array(3) { [0]=> string(6) “London” [1]=> string(5) “Paris” [2]=> string(8) “New York” }

This output shows the data type of each element, such as a string of 6 characters, in addition to the key and value.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals

Related Posts

What is Arrays and use cases of Arrays?

What is Arrays? An array is a data structure that stores a collection of elements of the same data type in a sequential manner. Each element in…

Read More

Python Example Code: Find the pair of adjacent elements that has the largest product

Rajesh Kumar I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have…

Read More

Python Example Code: Program for printing k largest elements in an array

Rajesh Kumar I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have…

Read More

Python Tutorials: Difference between List & Array & Tuple & Set & Dict

List Array Tuple List is mutable Array is mutable Tuple is immutable A list is ordered collection of items An array is ordered collection of items A…

Read More

Python Tutorials: Data Types – List

Rajesh Kumar I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have…

Read More

What is Numpy and How it works? An Overview and Its Use Cases ?

What is Numpy ? NumPy is the central bundle for logical registering in Python. A Python library gives a complex cluster object, different determined objects (like concealed…

Read More