johnwayne77 Posted January 17, 2007 Share Posted January 17, 2007 i am trying to make a currency converter script.i fetch the rss data from a daily updated currency source.I can echo as many currencies as I want but I don't know how to put each in its own variable (so I can use it for conversion in another script).[code] $items = array_slice($rss->items, 0, 2);foreach ($items as $item ) { echo ' <div align="center"> <div align="center">1 '.$item['title'].' = '.$item['description'].' RON<br /> </div> <hr width="60%" /> </div>'; }[/code]Now, I would like to do something like this:$variable1 = $item['title'][0]$variable2 = $item['description'][0]$variable3 = $item['title'][1]$variable4 = $item['description'][1]do u have any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/34559-foreach-puttin-each-result-in-its-own-variable/ Share on other sites More sharing options...
johnwayne77 Posted January 17, 2007 Author Share Posted January 17, 2007 if i do this:[code] $variable1 = $item['description']; $variable2 = $item['description']; $variable3 = $item['title']; $variable4 = $item['title'];echo $variable1;echo $variable2;echo $variable3;echo $variable4;[/code]i get this:[b]2.63222.6322USDUSD3.39983.3998EUREUR[/b]it seems it doubles the result, i only need 1 result per variable so I can use it after that with other scripts Quote Link to comment https://forums.phpfreaks.com/topic/34559-foreach-puttin-each-result-in-its-own-variable/#findComment-162778 Share on other sites More sharing options...
johnwayne77 Posted January 17, 2007 Author Share Posted January 17, 2007 I'm wondering... is there an echo like command to print only a specified numbers of characters from a string?like if i have $variable1 = 123456789 , i want to echo only the first 5 digits (12345) Quote Link to comment https://forums.phpfreaks.com/topic/34559-foreach-puttin-each-result-in-its-own-variable/#findComment-162781 Share on other sites More sharing options...
kenrbnsn Posted January 17, 2007 Share Posted January 17, 2007 You already have the information in an array, so use the array instead of making more variables.As for the answer to your other question, yes there are many functions to manipulate strings, read the section [url=http://www.php.net/strings]on strings[/url] in the manual.Ken Quote Link to comment https://forums.phpfreaks.com/topic/34559-foreach-puttin-each-result-in-its-own-variable/#findComment-162791 Share on other sites More sharing options...
johnwayne77 Posted January 17, 2007 Author Share Posted January 17, 2007 how do I do that? I need to be able to carry the results to other scripts also, that's why I thought about variables Quote Link to comment https://forums.phpfreaks.com/topic/34559-foreach-puttin-each-result-in-its-own-variable/#findComment-162796 Share on other sites More sharing options...
kenrbnsn Posted January 17, 2007 Share Posted January 17, 2007 What do you mean by "carry the results to other scripts"?Ken Quote Link to comment https://forums.phpfreaks.com/topic/34559-foreach-puttin-each-result-in-its-own-variable/#findComment-162801 Share on other sites More sharing options...
johnwayne77 Posted January 17, 2007 Author Share Posted January 17, 2007 After I get the results (e.g. USD, EUR, 1.21, 1.45) ,which are the currencies and the rates, and I thought of carrying them like this: $var1 = USD, $var2, EUR and so on(. i don't know how to do that directly from arrays regarding my script.), I would use them in mathematical operations for a currency conversion script.Am I clear enough with the explanation? Quote Link to comment https://forums.phpfreaks.com/topic/34559-foreach-puttin-each-result-in-its-own-variable/#findComment-162805 Share on other sites More sharing options...
johnwayne77 Posted January 17, 2007 Author Share Posted January 17, 2007 so, any ideas how can I solve this issue? I would really appreciate the help Quote Link to comment https://forums.phpfreaks.com/topic/34559-foreach-puttin-each-result-in-its-own-variable/#findComment-162859 Share on other sites More sharing options...
johnwayne77 Posted January 17, 2007 Author Share Posted January 17, 2007 how do I substitute foreach with a for syntax:like instead of this code:$items = array_slice($rss->items, 0, 2);foreach ($items as $item )i would need a new code to handle only the first two values like for $items = 1 do '...' and for $items = 2 do '...'any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/34559-foreach-puttin-each-result-in-its-own-variable/#findComment-162865 Share on other sites More sharing options...
kenrbnsn Posted January 17, 2007 Share Posted January 17, 2007 Please show us a dump of the array:[code]<?phpecho '<pre>' . print_r($items,true) . '</pre>';?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/34559-foreach-puttin-each-result-in-its-own-variable/#findComment-162872 Share on other sites More sharing options...
johnwayne77 Posted January 17, 2007 Author Share Posted January 17, 2007 this is it:[b]Array( [0] => Array ( [title] => USD [link] => http://www.infovalutar.ro/2007/1/17/USD.bnr [description] => 2.6322 [guid] => http://www.infovalutar.ro/azi/USD.bnr [summary] => 2.6322 ) [1] => Array ( [title] => EUR [link] => http://www.infovalutar.ro/2007/1/17/EUR.bnr [description] => 3.3998 [guid] => http://www.infovalutar.ro/azi/EUR.bnr [summary] => 3.3998 ))[/b] Quote Link to comment https://forums.phpfreaks.com/topic/34559-foreach-puttin-each-result-in-its-own-variable/#findComment-162874 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.