SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 484 | Next

James Kennard

"Mastering Joomla! 1.5 Extension and Framework Development"

filesystem.file');
if (false === ($data = JFile::read($CSV_FilePath)))
{
// handle failed to read CSV file
}
??? ??? ??? ???
Utilities and Useful Classes
[ 356 ]
Once we have retrieved the data we need to split it into an array of lines. We then
need to convert each line into an object. If we do not use objects, we will be unable to
use the JArrayHelper sorting method.
To create the objects, we use the toObject() method. This method creates a new
object and adds properties to the object based on the array keys. In this example,
when we use the toObject() method, the resultant objects will be of type stdClass
and have two keys??”id and value:
// convert CSV data into an array of lines
$data = explode("\n", $data);
// iterate over each line
for($i = 0, $c = count($data); $i < $c; $i ++)
{
// split the values
$temp = explode(',', $data[$i]);
// cast all the values to integers (always rounds down)
JArrayHelper::toInteger($temp);
// set the named values
$temp['id'] = $temp[0];
$temp ['value'] = $temp[1];
// remove keys 0 and 1
unset($temp[0], $temp[1]);
// convert the array to an object
$data[$i] = JArrayHelper::toObject($temp);
}
The first JArrayHelper method that we use in this example is toInteger().


Pages:
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496