Arrays are an integral part of PHP and we constantly use them when building Joomla! extensions. PHP provides us with a number of very useful functions for working with arrays. We can use the static JArrayHelper
class to simplify other common tasks when working with arrays.
The JArrayHelper
class is located in the joomla.utilities.arrayhelper
library. Before we can use the JArrayHelper
class we must import the relevant library:
jimport('joomla.utilities.arrayhelper');
Imagine we have a CSV file, which holds records with mathematical data:
2, 4.6 0, 0.0 1, 2.5 4, 8.2
Now imagine we want to order the data by ID (the first field) and we want the values (second field) to be displayed as integers.
The first thing we need to do is retrieve the contents of the CSV file; we do this using the JFile
class, discussed earlier in this chapter:
jimport('joomla.filesystem.file'); if (false === ($data = JFile::read($CSV_FilePath))) { // handle failed to read CSV file }
Once we have retrieved the data we...