jamesxg1 Posted February 12, 2010 Share Posted February 12, 2010 Hiya peeps! I cant figure this out for love nore money! LOL! I was wondering how I would split and array up into peices or strings without foreach() . Many thanks James. Link to comment https://forums.phpfreaks.com/topic/191919-how-do-i-split-an-array-into-a-string-without-using-foreach/ Share on other sites More sharing options...
jl5501 Posted February 12, 2010 Share Posted February 12, 2010 implode() Link to comment https://forums.phpfreaks.com/topic/191919-how-do-i-split-an-array-into-a-string-without-using-foreach/#findComment-1011555 Share on other sites More sharing options...
jamesxg1 Posted February 12, 2010 Author Share Posted February 12, 2010 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 https://forums.phpfreaks.com/topic/191919-how-do-i-split-an-array-into-a-string-without-using-foreach/#findComment-1011565 Share on other sites More sharing options...
Psycho Posted February 12, 2010 Share Posted February 12, 2010 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 https://forums.phpfreaks.com/topic/191919-how-do-i-split-an-array-into-a-string-without-using-foreach/#findComment-1011571 Share on other sites More sharing options...
jamesxg1 Posted February 12, 2010 Author Share Posted February 12, 2010 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 https://forums.phpfreaks.com/topic/191919-how-do-i-split-an-array-into-a-string-without-using-foreach/#findComment-1011576 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.