PDA

View Full Version : Some help with PHP arrays...



philsbury
January 2nd, 2009, 06:25 PM
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, item2

Any ideas?

Thanks
Phil

Templarian
January 2nd, 2009, 07:19 PM
^I think you mistyped that, did you mean item2, item2? or item2?



$str = "";
foreach($arr as $key => $value)
{
$str .= str_repeat($key, $value).", ";
}
$str = substr($str, 0, strlen($str) - 2);
echo $str;

philsbury
January 2nd, 2009, 07:28 PM
Sorry yes, should have just been 1 instance of item2.
Thanks Templarian, this works a treat!