Online
 
Friday, 09 January 2009
 
 
More article:
Related Content:

Implementing Arrays
 
Article Index
Implementing Arrays
Page 2
Page 3
Page 4
Page 5
Page 6
Page 7
Page 8
Page 9
Page 10
<?php

$petshack = array('Rosco' => array('name' => 'John',
'weight' => 20,
'animal' => 'Dog'),
'Icky' => array('name' => 'Ann',
'weight' => 10,
'animal' => 'Cat'),
'Rex' => array('name' => 'Cliff',
'weight' => 3,
'animal' => 'Iguana'),
'Buster' => array('name' => 'Amy',
'weight' => 54,
'animal' => 'Dog'),
'Delta' => array('name' => 'Hollie',
'weight' => 30,
'animal' => 'Dog'));
?>

The problem with an array structure such as this is it does not lend itself well to sorting the array by a particular column. As shown in , to take advantage of PHP's native sorting functionality, each column must be stored in its own separate array (keeping the key values synchronized so that index 1 for each array column corresponds to the appropriate value).

Listing 2.15. Creating a Sortable Table Using Arrays
<?php

$petshack['name'] = array('Rosco', 'Icky', 'Rex', 'Buster', 'Delta');
$petshack['owner'] = array('John', 'Ann', 'Cliff', 'Amy', 'Hollie');
$petshack['weight'] = array(20, 10, 3, 54, 30);
$petshack['animal'] = array('Dog', 'Cat', 'Iguana', 'Dog', 'Dog');
?>

Compare and ; the first thing you'll probably notice is that the second listing appears much cleaner than the original counterpart. Furthermore, because each column in the table is represented by a different PHP array, each column can also be sorted using PHP's internal array functions. To accomplish this sorting, you can use the asort() PHP function.


Tags: Add more tags...,
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)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Mobile Wallpaper 72
Statistic


Last Post

 
Top! Top!