preload
FunCap, Capcha Q & A Verification Text Thumbnails Generator Class
Feb 27

Arrays are one of the most used properties in php, arrays are like a container they are very simple they can can be indentified by a key or a value and the value can also be an array and that's called a multi-dimentional array.
Creating an array:

<?
$myArray = array("Value1", "Value2");
?>

By default when you specify the value of an array key the value of the key will increment starting from zero.
E.g.

[example]
myArray array
(
[0] = "Value1";
[1] = "Value2";
)
[/example]

You can also choose what you would like the key to be in an array.
e.g.

<?
$myArray = array("apple" => 1, "banana" => 3);
?>

And the array would now look like this with strings as the key not incrementing intergers.

[example]
myArray array
(
[apple] = 1;
[banana] = 3;
)
[/example]

You can also assign to an array like so:

<?
$myArray[] = 223What is my name?224;
?>

And you can also specify the value of the key for each item of an array.

<?
$myArray[223my_name224] = 223What is my name?224;
?>

And it will look something like this.

[example]
myArray array
(
[my_name] = 223What is my name?224
)
[/example]

There are many functions that intergrate with arrays and make them very useful, you also need to understand arrays when working with databases and PHP because php uses functions such as mysql_fetch_assoc and this function returns an array of information with all the fieldnames as the kays and the value are the values of the field.
So what can you do with an array?
You can make things appear on a random scema.
e.g.

<?
$myArray[] = 223Hell there224;
$myArray[] = 223Do you need224;
$myArray[] = 223A website?224;

echo $myArray[rand(0, count($myArray)-1)];
?>

This will output a random item from an array.

You can optimize code especially when working with databases by storing the data and calling it later and using it more then once.

Multidimensional arrays are also useful, i have worked with many of these and some of them use many levels and these such arrays are used with RSS Feeds and XML Feeds and that's also another reason to learn arrays!

$myArray = array('Teachers' => array(0 => 'Ben', 1 => 'Joe', 2 => 'Bob'), 'Students' => array('Stephen' => 0, 'Alex' => 2, 'Mike' => 2, 'Smith' => 1, 'Neo' => 2));

Now in this array we have the teachers and the students all stoed with their id's and the students also store the teacher id, so when retrieving the information we can display the students teachers name and or join group them together.

?>

[example]
Array
(
[Teachers] => Array
(
[0] => Ben
[1] => Joe
[2] => Bob
)

[Students] => Array
(
[0] => Array
(
[Stephen] => 0
)

[1] => Array
(
[Alex] => 2
)

[2] => Array
(
[Mike] => 2
)

[3] => Array
(
[Smith] => 1
)

[Neo] => 2
)

)
[/example]

So in PHP if we did something like this:

<?php

$myArray = array('Teachers' => array(0 => 'Ben', 1 => 'Joe', 2 => 'Bob'), 'Students' => array('Stephen' => 0, 'Alex' => 2, 'Mike' => 2, 'Smith' => 1, 'Neo' => 2));

foreach ($myArray['Teachers'] as $teacherID => $teacherName)
{
echo "<h2>Teacher '{$teacherName}'</h2>";

foreach ($myArray['Students'] as $studentName => $stendentTeacherID)
{
if ($stendentTeacherID == $teacherID)
{
echo "<h3>{$studentName}</h3>";
}
}
echo '<hr />';
}

?>

I hope this helps. Thanks for listening

Leave a Reply

 
 
Please enter the word above into the box below, the word is not case sensitive.
 
If you have trouble reading the word above then click here to load a new word.
Security word: