Welcome Guest, Not a member yet? Register   Sign In
Re-arranging array of array
#2

(This post was last modified: 03-12-2018, 06:15 AM by chisler.)

I've no idea how you are going to set the zero values but the below should display the totals for each 'serviced' type by company.

Code:
$data = array(
       array('name'=>'Test 1 Corp', 'serviced'=>'Visual help', 'price'=>'100.00' , 'serviced_date'=>'01/01/2018'),
       array('name'=>'Test 2 Corp', 'serviced'=>'Reading help', 'price'=>'50.00' , 'serviced_date'=>'01/2/2018'),
       array('name'=>'Test 1 Corp', 'serviced'=>'Visual help', 'price'=>'100.00','serviced_date'=>'01/02/2018'),
       array('name'=>'Test 3 Corp', 'serviced'=>'Visual help', 'price'=>'100.00', 'serviced_date'=>'01/10/2018'),
       array('name'=>'Test 1 Corp', 'serviced'=>'Misc help', 'price'=>'50.00'),
       //.... could be more
    );

   $reduced  = array();
   foreach($data as $company)
   {
     if (!array_key_exists($company['name'], $reduced)){
       $reduced[$company['name']] = array();
     }

     if (!array_key_exists($company['serviced'], $reduced[$company['name']])) {
       $reduced[$company['name']][$company['serviced']] = 0;
     }

     ++$reduced[$company['name']][$company['serviced']];
   }
Reply


Messages In This Thread
Re-arranging array of array - by ciadvantage - 03-10-2018, 09:58 AM
RE: Re-arranging array of array - by chisler - 03-11-2018, 03:59 PM
RE: Re-arranging array of array - by ciadvantage - 03-12-2018, 08:40 PM
RE: Re-arranging array of array - by chisler - 03-13-2018, 09:05 AM



Theme © iAndrew 2016 - Forum software by © MyBB