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.

 

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

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.