Monday, May 9, 2016

WDP - Arrays

PHP Arrays

An array is a special variable, which can hold more than one value at a time.
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 (syntax):
array();

-------------------------------------------------------------------------------------
array(
    key  => value,
    key2 => value2,
    key3 => value3,
    ...
)
If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
<?php
$color1 = "Red";
$color2 = "Green";
$color3 = "Blue";

echo $color1;
echo "<br>";

echo $color2;
echo "<br>";

echo $color3;
?>

---------------------------------------------------------------------------------
Output:

Red
Green
Blue
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

The index can be assigned automatically (index always starts at 0), like this:

<?php

$colors = array("Red", "Green", "Blue");

// Printing array structure
print_r($colors);
?>

This is equivalent to the following example, in which indexes are assigned manually:

<?php
$colors[0] = "Red"; 
$colors[1] = "Green"; 
$colors[2] = "Blue";

// Printing array structure
print_r($colors); 

//Printing one by one
echo "Value is $colors[1] <br/>";

echo "Value is $colors[2] <br/>";
?>
Output: Array ( [0] => Red [1] => Green [2] => Blue )
Value is Green 
Value is Blue 


Associative arrays are arrays that use named keys that you assign to them.
There are two ways to create an associative array: 
<?php
$ages = array("Peter"=>22, "Clark"=>32, "John"=>28);

// Printing array structure
print_r($ages); 
?>

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

// Printing array structure
print_r($ages); 

echo "<br/>";
echo "Age of Peter is ". $ages["Peter"];
?>
OutputArray ( [Peter] => 22 [Clark] => 32 [John] => 28 )
Array ( [Peter] => 22 [Clark] => 32 [John] => 28 ) 
Age of Peter is 22


A multidimensional array is an array containing one or more arrays.
PHP understands multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.

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 nested 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"];
?>
Output: Peter Parker's Email-id is: peterparker@mail.com



No comments:

Post a Comment