String to array then back to string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

String to array then back to string

Post by GeXus »

I'm trying to take a string and put it into an array (done that), now i'm looping through the array using foreach and replacing certain values (got that), now how do I put this back into a string? It's creating like a multidimential array, which is not what I want.. im kinda at a loss
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Can you post your array? Most often if you want to store as an array as a string, you'd use serialize(), or for simple arrays you can use implode(). For more complex arrays, you'll likely need to build the string yourself using concantation (sp?)
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Code: Select all

foreach($getBeginning as $word){
			$original = $word;
			$word = mysql_escape_string($word);
			$db->sql_query("select synonym from synonyms where word = '$word' limit 1");
			while($row = $db->fetch_array()){
				$synonym = $row['synonym'];
				$synonymWord = str_replace($word, $synonym, $synonymWord);
				
			}
			$new[$original] = $synonymWord;
			
		}
		print_r($new);
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you forgot to include the array, but it looks like a simple implode() should suffice.
Last edited by John Cartwright on Sat Mar 17, 2007 8:51 pm, edited 1 time in total.
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

sorry :)

Code: Select all

$getBeginning = explode(" ", $getBeginning);
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

just so you don't miss it, I editted my last post.
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Your right, it works.. i was doing some extra crap in there that wasnt neccassary.. thanks!
Post Reply