Hi,
I need a bit of help with an array. I'm posting quantities through a form and picking it up as an array which when printed looks like this:
Array ( [item1] => 4 [item2] => 1 )
What I want to do is now extract that data and display it as a list similar to:
item1, item1, item1, item1, item2
Any ideas?
Thanks
Phil
Some help with PHP arrays...
Moderator: General Moderators
Re: Some help with PHP arrays...
Code: Select all
foreach ($array as $key => $value)
for ($i=0; $i<$value; $i++)
echo $key.", ";There are 10 types of people in this world, those who understand binary and those who don't
Re: Some help with PHP arrays...
Thanks this almost does exactly what I want, though is there a way to not have the final comma?
item1, item1, item1, item1, item2, to item1, item1, item1, item1, item2
picky I know!
Cheers
Phil
item1, item1, item1, item1, item2, to item1, item1, item1, item1, item2
picky I know!
Cheers
Phil
Re: Some help with PHP arrays...
philsbury wrote:Thanks this almost does exactly what I want, though is there a way to not have the final comma?
I knew you were going to say that - it's your homework
There are 10 types of people in this world, those who understand binary and those who don't
Re: Some help with PHP arrays...
Code: Select all
$str = "";
foreach($array as $key => $value)
{
$str .= str_repeat($key.",", $value);
}
$str = substr($str, 0, strlen($str) - 2);
echo $str;Re: Some help with PHP arrays...
no, thats not right, should be -1:
Code: Select all
$str = "";
foreach($array as $key => $value)
{
$str .= str_repeat($key.",", $value);
}
$str = substr($str, 0, strlen($str) - 1);