Jump to content

Insert Content of Array into one Variable?


Alex1646

Recommended Posts

I am making  a website where writers can share stories.

One of the fields is other stories.

So I used this code to make it a array with the links in each f the items.

$other_split = explode(',', $other_form);
$other_int = count($other_split);

$x = 0;
while ($x>$other_int)
{
$other_link[$x] = "<a href = '$other_split[$x]'> Chapter $x </a>";
}

How can I insert the data in that array into one non array variable.

 

$other_split = explode(',', $other_form);
$other_int = count($other_split);

$x = 0;
while ($x>$other_int)
{



$other_link[$x] = "<a href = '$other_split[$x]'> Chapter $x </a>";
}

$allLinks = implode("\s", $other_link); //this will put all links in string with space between each

No need to make this harder than it needs to be. When you loop through the results, instead of putting them back into the array just to implode them into a string - ust build the string within the loop. And why in the worl would you use a while() loop instead of foreach()?

 

$output = '';
foreach ( explode(',', $other_form) as $chapter => $href)
{
$output .="<a href=\"{$href}\"> Chapter {$chapter} </a>\n";
}

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.