Page 1 of 1

Array Problem, confused

Posted: Mon Mar 27, 2006 3:47 pm
by Red Blaze
This is the little piece of code I'm trying to get working.

Code: Select all

<?php
$priceids = $row_call_albums['priceids'];
$arr = array($priceids);
foreach ($arr as $value) {
   $value = $value * 2;
   echo $value;
}
?>
In this case, $priceids calls 1, 2, 3.

Now, it does call it, but it doesn't loop. It does 1x2, and stops there. But if I replace "$priceids" with "1, 2, 3", it works just fine. It multiplies 1x2, then 2x2, then 3x2.

The results with $priceids in the array is just "2". But the results for "1, 2, 3" in the array is "246".

Is there a way around this issue? Thanks in advance.

Posted: Mon Mar 27, 2006 3:54 pm
by s.dot
How many items are you expecting this to return?

Code: Select all

$priceids = $row_call_albums['priceids']; 
$arr = array($priceids);

Posted: Mon Mar 27, 2006 3:59 pm
by Red Blaze
scottayy wrote:How many items are you expecting this to return?

Code: Select all

$priceids = $row_call_albums['priceids']; 
$arr = array($priceids);
"1, 2, 3" is what $priceids is calling. That should be 3 items, no? Or is the array just making it one item?

EDIT:
I tried this to test it:

Code: Select all

<?php
$priceids = $row_call_albums['priceids'];
$arr = array($priceids);
print_r($arr);
/*foreach ($arr as $value) {
   $value = $value * 2;
   echo $value;
}*/
?>
This is my result:
Array ( [0] => 1, 2, 3 )

Posted: Mon Mar 27, 2006 4:20 pm
by s.dot
you'll have to do something like this

Code: Select all

<?php 
$priceids = $row_call_albums['priceids']; 
$arr = explode(", ",$priceids);
foreach ($arr as $value) { 
   $value = $value * 2; 
   echo $value; 
} 
?>

Posted: Mon Mar 27, 2006 4:31 pm
by Red Blaze
scottayy wrote:you'll have to do something like this

Code: Select all

<?php 
$priceids = $row_call_albums['priceids']; 
$arr = explode(", ",$priceids);
foreach ($arr as $value) { 
   $value = $value * 2; 
   echo $value; 
} 
?>
That did the trick, thank you.

So, from what I'm reading there, explode seperates them? I never ran into that one before.
EDIT: I see it! It stripped the commas! But why did it need to strip the commas? Wasn't array(1, 2, 3) suppose to be the same thing as array($priceids)? *scratches head in confusion*

Posted: Mon Mar 27, 2006 4:34 pm
by s.dot
Yes sir. Explode will split up a string at the specified point, making each piece an element in an array. :)