Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to turn an array into a variable 2

Status
Not open for further replies.

SPYDERIX

Technical User
Joined
Jan 11, 2002
Messages
1,899
Location
CA
Hi,

I have an array which I would like to turn into a variable including the commas.

Now I know I can extract the array and put commas inbetween each part to simply output it on the screen using this:

for ($i=0; $i<count($sub_service); $i++)
{
echo $sub_service[$i];
if ($i == count($sub_service)-1)
{
echo &quot;<BR><BR>&quot;;
}
else
{
echo &quot;, &quot;;
}
}

That works ok, but I want to turn the entire output of that for loop into a single variable that I can use within an echo.

How do I do that. Every time I call the array variable the output just says &quot;Array&quot; and doesn't show everything inside it.

Thanks alot!

Nate

mainframe.gif

 
$somevar=&quot;&quot;;
for ($i=0; $i<count($sub_service); $i++)
{
$somevar.= $sub_service[$i];
if ($i == count($sub_service)-1)
{
$somevar.=&quot;<BR><BR>&quot;;
}
else
{
$somevar.= &quot;, &quot;;
}
}


echo $somevar;

????

Known is handfull, Unknown is worldfull
 
glad to be of help!!! :)

Known is handfull, Unknown is worldfull
 
Ok guys,

Why reinvent the wheel?
PHP has powerful functions to handle array->string and vice versa. Here's an example:
Code:
$myArray[0] = 'Apple';
$myArray[1] = 'Pear';
$myArray[2] = 'Prune';

$fruits = implode(',',$myArray);
# OR
echo(implode(',',$myArray));

Learn to use implode and explode will help not to blow up your code!
Cheers,
DRJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top