This
method casts all of the values in the $temp array into integers.
The objects created in the above example are of type stdClass. If we want to, we can
specify a different class. This example demonstrates how we would create objects of
type JObject:
$data[$i] = JArrayHelper::toObject($temp, 'JObject');
The class that we specify must not have any constructor parameters, or all the
constructor parameters must be optional.
If we ever need to convert an object back to an array, we can use the
fromObject() method:
$array = JArrayHelper::fromObject($object);
Chapter 12
[ 357 ]
Now that we have an array of objects we can start to play around with that array.
The first thing we'll do is sort the array by the ID of each record. We do this using the
sortObjects() method:
JArrayHelper::sortObjects($data, 'id');
By default this method sorts the data in ascending order; if we want to sort the data
in descending order, we must supply the third optional parameter set to -1:
JArrayHelper::sortObjects($data, 'id', -1);
This describes the resultant array when using all of the previous code with the
example CSV file; notice that each element is a stdClass object, all attributes of the
objects are integers, and the objects are in order of ID:
Array
(
[0] => stdClass Object
(
[id] => 0
[value] => 0
)
[1] => stdClass Object
(
[id] => 1
[value] => 2
)
[2] => stdClass Object
(
[id] => 2
[value] => 4
)
[3] => stdClass Object
(
[id] => 4
[value] => 8
)
)
The next thing that we will do is determine the total of the values.
Pages:
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497