Jump to content

How do I split an array into a string without using foreach ?


jamesxg1

Recommended Posts

implode()

 

Thanks for the reply mate! Only problem im having is my array goes a little something like this.

 

array('HOME' => 'home', 'TITLE' => 'title');

 

Heres what I have

 

		foreach($parts as $part => $value):
	echo str_replace('{' . $part . '}', $value, $file) . '<br /><br />';
endforeach;

 

But if there is 2 items in an array the loop is ran twice :S.

 

Many thanks

 

James.

Link to comment
Share on other sites

Isn't that what a loop i supposed to do? Why are you thinking that foreach would be a bad solution?

 

However, based upon the code posted I am assuming you are wanting to do multiple string replacements. If so, then you can use arrays as the search and replacement values. The problem you have with the current setup is that your actual search values are enclosed in brackets. So, you would have to modify the values of your search values.

 

$search = array_keys($parts);
foreach($search as $key=>$value)
{
    $search[$key] = "{".$value."}";
}

echo str_replace($search, $parts, $file) . '<br /><br />';

Link to comment
Share on other sites

Isn't that what a loop i supposed to do? Why are you thinking that foreach would be a bad solution?

 

However, based upon the code posted I am assuming you are wanting to do multiple string replacements. If so, then you can use arrays as the search and replacement values. The problem you have with the current setup is that your actual search values are enclosed in brackets. So, you would have to modify the values of your search values.

 

$search = array_keys($parts);
foreach($search as $key=>$value)
{
    $search[$key] = "{".$value."}";
}

echo str_replace($search, $parts, $file) . '<br /><br />';

 

Spot on cheers for that mate! I must admit I was doubting the foreach() for a while but nope it seems my knolodge is minimal LOL!

 

Cheers mate take care

 

Many thanks

 

James.

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.