Array Problem, confused

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Array Problem, confused

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

How many items are you expecting this to return?

Code: Select all

$priceids = $row_call_albums['priceids']; 
$arr = array($priceids);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Post 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 )
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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; 
} 
?>
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Post 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*
Last edited by Red Blaze on Mon Mar 27, 2006 4:35 pm, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Yes sir. Explode will split up a string at the specified point, making each piece an element in an array. :)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply