Jump to content

Recommended Posts

The following code:

for ($i = 0; $i < $varcount; $i = $i + 1) {echo "," . "\$parts[$i]";}

 

Will output:

,$parts[0],$parts[1],$parts[2],$parts[3],$parts[4],$parts[5],$parts[6],$parts[7],$parts[8]

 

What is a good way to append or save the resulting output to a variable?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/55644-solved-loop-through-and-append-to-string/
Share on other sites

If you are dealing with an array and you want to place each array item into a string to be saved to a variavle use implode:

 

<?php

$parts = array('nuts and bolts', 'spanners', 'a chain');

$var = implode(', ', $parts);

echo $var;

?>

The result is a nice list like so:

nuts and bolts, spanners, a chain

Not quite what I'm looking for.  I want a string because it will be used for a printr function like so:

 

printf($cfgcontents . "\r",$parts[0],$parts[1],$parts[2],$parts[3],$parts[4],$parts[5],$parts[6],$parts[7],$parts[8])

 

so when it goes through the loop, I want each part appended to the string:

 

$parts[0]....then $parts[1]...etc....

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.