Saturday, July 18, 2009

PHP Arrays

In PHP, there are three kind of arrays:

* Numeric array - An array with a numeric index
* Associative array - An array where each ID key is associated with a value
* Multidimensional array - An array containing one or more arrays

--------------------------------------------------------------------

Numeric Arrays


A numeric array stores each array element with a numeric index.

<html>
<body>
<?php
$carII=array("Saab","Volvo","BMW","Toyota");

$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";

//echo $carll[0] . " and " . $carll[1] . " are Swedish cars.;
echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
?>

</body>
</html>

-------------------------------------------
Associative Arrays

An associative array, each ID key is associated with a value.

When storing data about specific named values, a numerical array is not always the best way to do it.

With associative arrays we can use the values as keys and assign values to them.


<html>
<body>

<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";

echo "Peter is " . $ages['Peter'] . " years old.";
?>

</body>
</html>

---------------------------------------------------------
Multidimensional Arrays

In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.


<html>
<body>
<?php
$families = array
(
"Griffin"=>array
(
"Peter",
"Lois",
"Megan"
),
"Quagmire"=>array
(
"Glenn"
),
"Brown"=>array
(
"Cleveland",
"Loretta",
"Junior"
)
);
echo "Is " . $families['Griffin'][2] .
" a part of the Griffin family?";
?>

</body>
</html>


The array above would look like this if written to the output:
Array
(
[Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
)

---------------------------------------------------
For Loop

<html>
<body>

<?php
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "< br /> ";
}
?>

</body>
</html>
--------------------------------------
The foreach Loop

The foreach loop is used to loop through arrays.

<html>
<body>

<?php
$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br /> ";
}
?>

</body>
</html>

-----------------------------------------------------
Create a PHP Function

A function will be executed by a call to the function.

<html>
<body>

<?php
function writeName()
{
echo "Kai Jim Refsnes";
}

echo "My name is ";
writeName();
?>

</body>
</html>

------------------------------------------------------
PHP Functions - Adding parameters

To add more functionality to a function, we can add parameters

<html>
<body>

<?php
function writeName($fname)
{
echo $fname . " Refsnes.<br />";
}

echo "My name is ";
writeName("Kai Jim");
echo "My sister's name is ";
writeName("Hege");
echo "My brother's name is ";
writeName("Stale");
?>

<body>
<html>

-------------------------------------------------
PHP Functions - Return values

To let a function return a value, use the return statement.

<html>
<body>
<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
}

echo "1 + 16 = " . add(1,16);
?>

</body>
</html>