RSS

Arrays

Sun, Jan 17, 2010

PHP & MySql

In PHP you can also create special variables called arrays. They can be used to store multiple values into in multiple ’sub-variables’ as I like to call them, which each have its own index (‘name’) and value. So they can contain a collection of multiple variables. An array can be written in 2 different ways which are both exactly the same:

Example 1

$array_variable_name = array("index_name" => "value",
                   "index_name2" => "value2");

Example 2

$array_variable_name["index_name"] = "value";
$array_variable_name["index_name2"] = "value2";

However when you need to add another ’sub-variable’ to the array, you should use the second method. That method just defines a sub-variable of the array (adds it to the array) while the first method defines the array with ALL sub-variables it should contain.

Let’s compare them with normal variables by showing a way how you could define 2 variables, and how you could define them but then put together in an array.

Variables seperated:

$var_name = "var value";
$var_name2 = "var value2";

Array containing the variables:

$array_name['var_name'] = "var value";
$array_name['var_name2'] = "var value2";

To echo a variable of an array, you can just use the echo method:

echo $array['var'];

Although you can not echo an array itself (as an array contains multiple sub-variables and so multiple values, so it doesn’t know which sub-variable’s value to display/use).
You can however use the print_r function to display a full array with all its sub-variables and values.

print_r($array);

One more example of an array and the outputs:

$product_prices = array("tv" => 599.95,
              "computer" => 899.95,
                         "notebook" => 999.999
                      );

echo $product_prices["tv"]; //returns: 599.95

echo $product_prices["computer"]; //returns: 899.95
echo $product_prices["notebook"]; //returns 999.999
//we can also change the value of a sub-variable from the array and display the new value
$product_prices["notebook"] = 799.999;
echo $product_prices["notebook"]; //now returns: 799.999

An array can as well contain ANOTHER array.

Example:

<?php

$products[1] = array("name" => "tv", "price" => 599.95, "available" => "yes");
$products[2] = array("name" => "computer", "price" => "899.95", "available" => "no");

foreach( $products as $number => $sub_array ) {

   echo "Product #{$number}: ";

   foreach ( $sub_array as $key => $value) {

          echo $key.": ".$value." ";

   }

   echo "<p>";

}

?>

The array $products now contains 2 other arrays. One array that contains sub-variables ‘name’, ‘price’ and ‘available’, and a second array that contains those sub-variables as well but then for another product ( other values for it ). The value of the arrays and sub-variables could be written like this:

//$products = array(1 => array(), 2 => array();
//$products[1] = array(), $products[2] = array();
$products[1]['name'] = "tv";
$products[1]['price'] = 599.95;
$products[1]['available'] = "yes";

$products[2]['name'] = "notebook";
$products[2]['price'] = "899.95";
$products[2]['available'] = "no";

Admin.