Jump to content

[SOLVED] Loop Through and Append to String


TripleDES

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....

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.