...

View Full Version : Resolved Help adding values to elements in an array please?



stfc_boy
09-15-2011, 10:06 AM
Hello All,

Wondering if you can help please. If I have an array:



print_r($array);


Which outputs like



one,two,three,four,five


I wanted to dynamically add quote marks around each element in the $array like the below, how could I do it?



"one","two","three","four","five"


Thank you in advance

Wanna
09-15-2011, 10:13 AM
This example is for if you do make the array yourself


$array = array("one","two","three");
foreach($array as $key => $value)
{
$array[$key] = '"'.$value.'"';
}

print_r($array);


If you can make the array yourself:


$array = array('"one"','"two"','"three"');

print_r($array);

stfc_boy
09-15-2011, 10:31 AM
Thanks for your help Wanna,

But unfortunately i'm calling this data in from a database which is then being held in an array rather than creating the array myself, so it is returned without quotes. I'm therefore still not too sure how I could reach my goal with quotes wrapped around each element in the array.

Thanks

stfc_boy
09-15-2011, 10:40 AM
Hi Wanna,

Had a think again and used a foreach loop as you mentioned in your first example and got it working.

Thanks for your help again.

Wanna
09-15-2011, 10:41 AM
Iīm sorry for my mistake.

The first code i posted i for an array you donīt make youself.



$array = array("one","two","three");
foreach($array as $key => $value)
{
$array[$key] = '"'.$value.'"';
}

print_r($array);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum