PHP: limit items to 10 when filling an array -


i using following create two-dimensional array simplexml string, works fine far:

$dataraw = array();  foreach($objranking->history $history) { if($history->groupname == "currentmonth") {     $dataraw[(string)$history->groupname->item] = (int)$history->groupname->groupcount; }} 

the array looks follows:

array ( [item1] => 2 [item2] => 3 [item3] => 5 [item4] => 7 [item5] => 11 [item6] => 13 [item7] => 17 [item8] => 19 [item9] => 23 [item10] => 29 [item11] => 31 [item12] => 37 ) 

is there way can limit maximum 10 items within array , summarise remaining items [others] value sum of remaining values ?

many this, mike.

initailize counter , keep on incrementing on each iteration. on each iteration, check if counter below 10 - if so, add data array $dataraw usual. when counter value above 10, start adding them new array ($restoftheitems here). once loop has finished, can create new index, sum array values , assign it.

$dataraw = array(); $restoftheitems = array();  $i = 0; foreach($objranking->history $history) {      if($i <= 10) {         if($history->groupname == "currentmonth") {             $dataraw[(string)$history->groupname->item] = (int)$history->groupname->groupcount;         }     } else {         $restoftheitems[] = (int)$history->groupname->groupcount;     }      $i++; }  $dataraw['others'] = array_sum($restoftheitems); 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -