spanish icon   Spanish version


english icon  English version

News

03-24-2009:PHP Regular Expressions tutorial added to the PHP tutorials section.


03-19-2009: Two old templates (Photoblue and Hardwarezip) added to the webpage.


03-01-2009:Preg and Pattern Functions tutorial added to the PHP tutorials section.


About Redacron:

We are the creators of:


Email Icon  Write to Us


Valid XHTML 1.0 Transitional


Links

Webmaster Toolkit Collection of webmaster tools developed to help webmasters with our daily webmaster chores.

Geobytes For the web developer, Geobytes content localization technology is 'rain maker' technology.


 


A Study in Arrays


Arrays are a broad topic in any programming language and PHP is no exception. Hence this tutorial will be divided in several parts, this one being the first. An array can be defined as an orderly arrangement, or as a large number of objects, persons, etc. An array in PHP is a set of values tied to keys.


An array can be constructed with the array language-construct, like in the following example:


<?php
$cars = array( "luxury_vehicle" => "Lexus" , "van" => "Dodge" );

echo $cars [ "luxury_vehicle"];
?>


In this case we have cars as an array. If you use echo to print out the value of $cars [ "luxury_vehicle"], you'll see Lexus appearing on the screen. Why? Because Lexus is one of the values in the array, and luxury_vehicle is the key. A key can be a string or an integer. If you use a float value as a key, PHP will turn it into an integer.


The syntax behind arrays may seem difficult at this point, but one of the good things about PHP is that it always makes things easier for all of us. If you were to type this,


<?php
$cars = array( 1 => "Lexus" , 2 => "Dodge" );
$cars[] = "BMW";
echo $cars [3]; //will print out BMW
?>


The script above uses numbers as keys. However, a third value is added outside the array, and this time there's no key in it. When this happens, PHP will use the number following the latest number inside the array, in this case the number is 3. That's why using the value of $cars [3] is BMW.


There's more to arrays than this. PHP is full of functions you can use to manipulate arrays in many ways.


There are three arrays in this script:



And we also put in the script this array declaration:
$robots = array ("Robocop", "Mazinger", "Data", "Sonny" );


Using a simple script with $ygor[2], we get:


<?php


function pruff ($txt, $size = 3)
{


print "<p><font color=\"0000aa\" size=\"$size\">$txt</font></p>";


}


$ygor[] = 1;
$ygor[] = 2;
$ygor[] = 3;


$robots = array ("Robocop", "Mazinger", "Data", "Sonny" );


pruff("The value of \$ygor[2] is $ygor[2].");
pruff("The value of \$robots[3] is $robots[3].");
pruff ("Using count(\$robots), we learn that the number of variables in the array is: ".count($robots));


?>



The script uses a function pruff to print out the values of the arrays. In this case we have two arrays: one is $robots, and the other one is $ygor. As you can see, $ygor was not declared using array, and the $robots array has no keys.


count is very useful when it comes to arrays, for it counts the number of variables inside an array.


And the output of the above script is:




The value of $ygor[2] is 3.
The value of $robots[3] is Sonny.
Using count($robots), we learn that the number of variables in the array is: 4


You may think arrays with keys (associative arrays) aren't needed. However, they are good for storing data.
Take a look at the following array:

$robots_spec = array ( name=>"Data", occupation=>"Starfleet Officer", age=>"Unknown",special-power=>"Rapid thinking");


Let's see the array at work in this script:


Go to Page 2.



Home

Services: Joomla/osCommerce | Search Engine Optimization | Logo Design | Web Design

Portfolio: Catalog Design | Logo and Banner Design | Banner and Header Portfolio | Web Design

Tutorials: All Tutorials



Copyright © 2008 Redacron Studios. Design by R.P Carbonell.