| Implementing Arrays |
| Article Index |
|---|
| Implementing Arrays |
| Page 2 |
| Page 3 |
| Page 4 |
| Page 5 |
| Page 6 |
| Page 7 |
| Page 8 |
| Page 9 |
| Page 10 |
Listing 2.12. Dynamic Generation of <IMG> Tags from an Array
<HTML>
<HEAD><TITLE>Using the array as a list</TITLE></HEAD>
<BODY>
<?php
$images = array('image1.jpg', 'image2.jpg', 'image3.jpg');
foreach($images as $val): ?>
<IMG src="/images/<?php echo $val; ?>">
<?php endforeach;?>
</BODY>
</HTML>
Taking this example a step further, how would you use an array to create a script that displays a single random image every time the script executes? To do so using an array, it is probably best that a new PHP function is introducedthe array_rand() function. The syntax of this function is as follows:
array_rand($input [, $num_desired])
As shown, the array_rand() function takes two parameters. The first $input is, as the name implies, the input array. The second optional parameter, $num_desired, is an integer identifying how many random elements to pick from the input array. If the second parameter is not specified, the function defaults to a single random element. When executed, this function returns either a scalar value representing the single key in the original array or an array list of keys picked from the original array.
NOTEIn PHP, anytime a random number is required (for instance, when using the array_rand() function) the srand() function must be called to properly seed the random number generator. |
With this knowledge in hand, implementing your random image script becomes trivial, as you can see in :
Listing 2.13. A Random-Image Script Using array_rand()
<HTML>
<HEAD><TITLE>A random image script</TITLE></HEAD>
<BODY>
<?php
srand((double)microtime()*1000000);
$images = array('image1.jpg', 'image2.jpg', 'image3.jpg');
$rImage = array_rand($images)
?>
<IMG src="<?php echo $images[$rImage]; ?>">
</BODY>
</HTML>
Using Arrays as a Sortable Table
Beyond simple lists, arrays within PHP are also very useful when you're working with and storing data that is designed to be in the form of a table. In this section, you'll learn how to implement simple tables using arrays with PHP, followed by a more complex example that is useful in developing array-based tables that can be sorted by an arbitrary column using PHP's array-sorting functions. Consider the following table:
|
Pet Name |
Owner Name |
Weight |
Animal |
|---|---|---|---|
|
Rosco |
John |
20 |
Dog |
|
Icky |
Ann |
10 |
Cat |
|
Rex |
Cliff |
3 |
Iguana |
|
Buster |
Amy |
54 |
Dog |
|
Delta |
Hollie |
30 |
Dog |
Take a look at the preceding table; how could you implement this table using arrays in PHP? The simplest method would be to create an associative array (based on the name of each pet) whose value is another array containing the remainder of the data for a particular row, as shown in :
Listing 2.14. Creating a Simple Table Using Arrays
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a comment.
Tags: Simple PHP, Pear, Easy PHP, PHP Tutorial, PHP MySQL, XSLT, Sap Tutorial, CSS Tutorial, XSL FO Java, SQL Tutorial.
| Users' Comments (0) |
|
No comment posted








