Welcome Guest, Not a member yet? Register   Sign In
Assign Variable to individual array item
#1

[eluser]tkaw220[/eluser]
My code below obtain sum from database group by currency:

Code:
public function get_sales_report()
{
    $data = $this->db
         ->select('currency')
         ->select_sum('order_amount')
         ->where('status', 'completed')
         ->group_by('currency')
         ->get('orders')->result();

    return $data;
}

Result:

Code:
Array
(
    [0] => stdClass Object
        (
            [currency] => SGD
            [order_amount] => 100.22
        )

    [1] => stdClass Object
        (
            [currency] => USD
            [order_amount] => 1041.32
        )
)

How do I assign the amount of each currency to a variable? For example, assign SGD amount to $amount_sgd, so that I can change it to USD value, and add it to USD amount (second array item) to get final total in USD currency.

Many thanks.
#2

[eluser]Seb[/eluser]
If you want to do it in PHP, you can write :

Code:
$sum = 0;
$exchange = array('SGD' => 0.81, 'USD' => 1, 'EUR' => 1.42); // add more currencies here
foreach($data as $sale)
{
    $sum += $exchange[$sale->currency] * $sale->order_amount;
}
// you get the result in $sum
#3

[eluser]tkaw220[/eluser]
Hi Seb,

I have only 3 currency. But they might not all appear at the same time (sometimes orders in one currency). Thus, I cannot prefix the array here. I wish to be able to extract the key and value from the array, and assign them to variable. I can reuse the variable for other purposes later.
#4

[eluser]Seb[/eluser]
Well, where is the problem with the code I wrote ?

It will work even if there are only orders in one currency. You just have to make sure the $exchange array is filled with all available currencies in your application.
#5

[eluser]tkaw220[/eluser]
Hi Seb,

Not. Your code is fine. But I wish to assign item from array to a variable. For example,

1) Extract the amount in USD and assign it to variable called $amount_usd.
2) I can called out the $amount_usd anytime within page for other purpose. For example,

Code:
Sales in USD currency: $amount_usd
#6

[eluser]blagi[/eluser]
[quote author="tkaw220" date="1309244276"]Hi Seb,

Not. Your code is fine. But I wish to assign item from array to a variable. For example,

1) Extract the amount in USD and assign it to variable called $amount_usd.
2) I can called out the $amount_usd anytime within page for other purpose. For example,

Code:
Sales in USD currency: $amount_usd
[/quote]


You can use:
$val = 'usd' ;
$amount[$val] ...

or
$val = 'usd';
$varNname = 'amount_'.$val;

$$varName = ... //$$varname is var $amount_usd

---
blagi
#7

[eluser]tkaw220[/eluser]
Hi blagi,


Sorry, I am still not getting your trick. Consider I have below multi-dimension array:

Code:
Array
(
    [0] => array
        (
            [currency] => SGD
            [order_amount] => 100.22
        )

    [1] => array
        (
            [currency] => USD
            [order_amount] => 1041.32
        )
)

How do I extract the USD and its value and assign it to a variable? Please note that the array key would change because sometimes only one currency returned and sometimes 3 currency.
#8

[eluser]osci[/eluser]
The array that is returned from the query is enough. You don't need any other vars. you simple would do a
Code:
(! isset($data['USD']) OR $data['USD'] == "") ? '' : $data['USD']
to get the value for USD.
#9

[eluser]tkaw220[/eluser]
Hi osci,

Thanks for code. Thank you very much.
#10

[eluser]toopay[/eluser]
No need to use 'isset()' ! Only
Code:
( ! $data['USD'] OR empty($data['USD'])) ? '' : $data['USD']




Theme © iAndrew 2016 - Forum software by © MyBB