![]() |
Assign Variable to individual array item - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Assign Variable to individual array item (/showthread.php?tid=43016) Pages:
1
2
|
Assign Variable to individual array item - El Forum - 06-27-2011 [eluser]tkaw220[/eluser] My code below obtain sum from database group by currency: Code: public function get_sales_report() Result: Code: Array 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. Assign Variable to individual array item - El Forum - 06-27-2011 [eluser]Seb[/eluser] If you want to do it in PHP, you can write : Code: $sum = 0; Assign Variable to individual array item - El Forum - 06-27-2011 [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. Assign Variable to individual array item - El Forum - 06-27-2011 [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. Assign Variable to individual array item - El Forum - 06-27-2011 [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 Assign Variable to individual array item - El Forum - 06-28-2011 [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 You can use: $val = 'usd' ; $amount[$val] ... or $val = 'usd'; $varNname = 'amount_'.$val; $$varName = ... //$$varname is var $amount_usd --- blagi Assign Variable to individual array item - El Forum - 06-28-2011 [eluser]tkaw220[/eluser] Hi blagi, Sorry, I am still not getting your trick. Consider I have below multi-dimension array: Code: Array 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. Assign Variable to individual array item - El Forum - 06-28-2011 [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'] Assign Variable to individual array item - El Forum - 06-28-2011 [eluser]tkaw220[/eluser] Hi osci, Thanks for code. Thank you very much. Assign Variable to individual array item - El Forum - 06-28-2011 [eluser]toopay[/eluser] No need to use 'isset()' ! Only Code: ( ! $data['USD'] OR empty($data['USD'])) ? '' : $data['USD'] |