Sunday 21 April 2013

PHP Arrays


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 values in a single value. For experienced programmers it is important to note that PHP's arrays are actually maps (each key is mapped to a value).

In PHP, there are three types of arrays:
  • Indexed arrays - Arrays with numeric index
  • Associative arrays - Arrays with named keys
  • Multidimensional arrays - Arrays containing one or more arrays

If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
$cars1="Volvo";

$cars2="BMW";
$cars3="Toyota";

However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?
The solution is to create an array!


1)A Numerically Indexed Array


·         If this is your first time seeing an array, then you may not quite understand the concept of an array. Imagine that you own a business and you want to store the names of all your employees in a PHP variable. How would you go about this?
·         It wouldn't make much sense to have to store each name in its own variable. Instead, it would be nice to store all the employee names inside of a single variable.

Example:


<!DOCTYPE html>
<html>
<body>


<?php
$cars=array("Volvo","BMW","Toyota"); 
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>



</body>
</html>



Output:

I like Volvo, BMW and Toyota.


2) PHP Associative Arrays:


In an associative array a key is associated with a value. If you wanted to store the salaries of your employees in an array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys in our associative array, and the value would be their respective salary.

Example:

$salaries["Bob"] = 2000;
$salaries["Sally"] = 4000;
$salaries["Charlie"] = 600;
$salaries["Clare"] = 0;

echo "Bob is being paid - $" . $salaries["Bob"] . "<br />";
echo "Sally is being paid - $" . $salaries["Sally"] . "<br />";
echo "Charlie is being paid - $" . $salaries["Charlie"] . "<br />";
echo "Clare is being paid - $" . $salaries["Clare"];


Output:

Bob is being paid - $2000

Sally is being paid - $4000
Charlie is being paid - $600
Clare is being paid - $0


3) PHP Multidimensional Arrays:


An array can also contain another array as a value, which in turn can hold other arrays as well. In such a way we can create two- or three-dimensional arrays:

 Example:


<!DOCTYPE html>

<html>
<body>

<?php
// A two-dimensional array
$cars = array
   (
   array("Volvo",100,96),
   array("BMW",60,59),
   array("Toyota",110,100)
   );
   
echo $cars[0][0].": Ordered: ".$cars[0][1].". Sold: ".$cars[0][2]."<br>";
echo $cars[1][0].": Ordered: ".$cars[1][1].". Sold: ".$cars[1][2]."<br>";
echo $cars[2][0].": Ordered: ".$cars[2][1].". Sold: ".$cars[2][2]."<br>";
?>

</body>
</html>

 Output:


Volvo: Ordered: 100. Sold: 96

BMW: Ordered: 60. Sold: 59
Toyota: Ordered: 110. Sold: 100

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More