Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Types of Array in PHP

 Types of Array in PHP

What is an Array?
An array is a special variable, which can hold more than one value at a time. An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.

If you have a list of items (a list of color names, for example), storing the colors in single variables could look like this:

$color1 = “Red”;
$color2 = “Blue”;
$color3 = “Green”;

However, what if you want to loop through the colors and find a specific one? And what if you had not 3 colors, but 300?
The solution is to create an array! An array can hold many values under a single name, and you can access the values by referring to an index number.

In PHP, the array() function is used to create an array.

Types of Arrays in PHP
There are three types of arrays that you can create. These are:

  1. Indexed array : An array with a numeric key.
  2. Associative arrays : Arrays with named keys
  3. Multidimensional arrays : Arrays containing one or more arrays

1. Indexed Arrays
There are two ways to create indexed arrays. The index can be assigned automatically (index always starts at 0), like this:
$colors = array(“Red”, “Blue”, “Green”);

or the index can be assigned manually:
$colors[0] = “Red”;
$colors[1] = “Blue”;
$colors[2] = “Green”;

The following example creates an indexed array named $colors, assigns three elements to it, and then prints a text containing the array values:

<?php
$colors = array(“Red”, “Blue”, “Green”);
echo “I like ” . $colors[0] . “, ” . $colors[1] . ” and ” . $colors[2] . “.”;
?>

2. 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);
?>

The following example is equivalent to the previous example, but shows a different way of creating associative arrays.

<?php
$ages[“Peter”] = “22”;
$ages[“Clark”] = “32”;
$ages[“John”] = “28”;
?>

3. 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” => “Nikunj Joshi”,
“email” => “contact@nikunjjoshiphpdeveloper.com”
),
array(
“name” => “Clark Kent”,
“email” => “clarkkent@mail.com”
),
array(
“name” => “Harry Potter”,
“email” => “harrypotter@mail.com”
)
);

// Access nested value
echo “Nikunj Joshi’s Email-id is: ” . $contacts[0][“email”];
?>

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(“Ahmedabad”, “Bangalore”, “Pune”);
// Display the cities array
print_r($cities);
?>

The print_r() statement gives the following output:
Array ( [0] => Ahmedabad [1] => Bangalore [2] => Pune )

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(“Ahmedabad”, “Bangalore”, “Pune”);
// Display the cities array
var_dump($cities);
?>

This var_dThis var_dump() statement gives the following output:
array(3) { [0]=> string(9) “Ahmedabad” [1]=> string(9) “Bangalore” [2]=> string(4) “Pune” }

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

If you have any query or suggestions, feel free to ask me via the comment section below.

must watch some other topics :

cake PHP training institute
cake PHP training course
cake php training online

Post a Comment

0 Comments