Jump to content

foreach -- puttin each result in its own variable


johnwayne77

Recommended Posts

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">&nbsp;&nbsp;
  <div align="center">1&nbsp;&nbsp;'.$item['title'].' &nbsp;&nbsp;=&nbsp;&nbsp; '.$item['description'].'&nbsp;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?
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.6322USDUSD
3.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
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
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?
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?
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.