We could
do this by iterating over the array and adding each value to the total. Another
way of achieving this is by using the getColumn() method and the array_sum()
function together:
$total = array_sum(JArrayHelper::getColumn($data, 'value'));
Utilities and Useful Classes
[ 358 ]
The getColumn() method is used to retrieve a column of data from an array
structure. In order for this method to work as expected, the array must be populated
with either objects or arrays.
Imagine we have an array of values of mixed types and we want to retrieve different
values from that array casting the values to the appropriate type as we do so. To do
this we can use the getValue() method:
$array = array(12, '1.3');
$value = JArrayHelper::getValue($array, 0, '', 'ALNUM')
The first parameter is the array from which we want to retrieve the value; the array
will be passed by reference. The second parameter is the name of the array element
key the value of which we want to get.
The third and fourth parameters are both optional.
Pages:
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498