Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Adam Schnare
PLUS
Adam Schnare
Courses Plus Student 889 Points

adding different array sub keys together

I'm trying to add different items in other arrays together.

[ItemDescription] => Pane [cost], [ItemDescription] => Screens [cost] and [ItemDescription] => Storm[cost].

I want my output totals to look like the following:

Panes, Screens, Storm total: $35.50 
Windows: $0.00 
Doors: $10.00 
 Array
(
    [aInvoiceArray] => Array
        (
            [UDT_RequestOutputEstimate] => Array
                (
                    [0] => Array
                        (
                            [ItemCode] => WIN_PANES
                            [ItemDescription] => Panes 
                            [Quantity] => 1
                            [Cost] => $15.50
                        )

                    [1] => Array
                        (
                            [ItemCode] => WIN_SCREENS
                            [ItemDescription] => Screens
                            [Quantity] => 1
                            [Cost] => $10.00
                        )

                    [2] => Array
                        (
                            [ItemCode] => WIN_STORMS
                            [ItemDescription] => Storm
                            [Quantity] => 1
                            [Cost] => $10.00
                        )

                    [3] => Array
                        (
                            [ItemCode] => WIN_GLIDERS
                            [ItemDescription] => Windows
                            [Quantity] => 1
                            [Cost] => $0.00
                        )
Sergey Podgornyy
Sergey Podgornyy
20,660 Points

Share code of your array please

Adam Schnare
Adam Schnare
Courses Plus Student 889 Points

there is nothing to share. I use a soap service to call the following array above.

<?php
$result = $client->call('GetJobEstimatewithHours', array('parameters' => $param), '', '', false, true); 
?>

when you print $result it gives you the array above.

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

Hm, I'm sorry, I don't know how to add currency strings

1 Answer

Mike Costa
PLUS
Mike Costa
Courses Plus Student 26,362 Points

One idea would be if you know the result from cost will ALWAYS have a $ at the beginning, you can create a function and pass in the cost value (which would be a string) and return a float.

function convertCostToFloat($cost){
   return floatval(substr($cost, 1));
}

So if you pass in convertCostToFloat('$12.99'), you'll get a float back. Then just increment accordingly.

// create your grouped array to check
$grouped_prices = ['Panes', 'Screens', 'Storm'];

// init storage for prices
$prices = [];
// init set for grouped prices
$prices['grouped'] = 0;

foreach($array['aInvoiceArray']['UDT_RequestOutputEstimate'] as $key => $value){
    if(in_array( $value['ItemDescription'], $grouped_prices)){
        $prices['grouped'] += convertCostToFloat($value['Cost']);
    } else {
        if(!isset($prices[$value['ItemDescription']])){
             $prices[$value['ItemDescription']] = 0;
        }
        $prices[$value['ItemDescription']] += $value['Cost'];
    }
}

// Output prices from array

Something like that maybe could help?