tastro Posted October 1, 2010 Share Posted October 1, 2010 hi, now i have something like this: $pizza = "piece1 piece2 piece3 piece4"; $one_word = explode(" ", $pizza); but i want to get 2 words together, like that: $two_words[0]='piece1 piece2'; $two_words[1]='piece3 piece4'; $two_words[2]='piece2 piece3'; $two_words[3]='piece1 piece4'; if it's impossible with explode, then it's fine if you use some other function, i don't care as long as it works. thank you, tastro Quote Link to comment https://forums.phpfreaks.com/topic/214952-need-to-get-2-words-together-from-explode-not-just-one-how-to-do-it/ Share on other sites More sharing options...
Psycho Posted October 1, 2010 Share Posted October 1, 2010 You could either explode it as you are and then loop through the array to concatenate them or you could use a regular expression. Here is how you can do it by using explode and a littel post processing $pizza = "piece1 piece2 piece3 piece4"; $words = explode(' ', $pizza); $twoWords = array(); for($i=0, $wordCount=count($words); $i<$wordCount; $i+=2) { $twoWords[] = $words[$i] . ((isset($words[$i+1])) ? " {$words[$i+1]}" : ''); } Quote Link to comment https://forums.phpfreaks.com/topic/214952-need-to-get-2-words-together-from-explode-not-just-one-how-to-do-it/#findComment-1118163 Share on other sites More sharing options...
tastro Posted October 1, 2010 Author Share Posted October 1, 2010 the regex version takes more memory right? if yes then i think that this one is better. tnx mate! i'm gonna test it right now. Quote Link to comment https://forums.phpfreaks.com/topic/214952-need-to-get-2-words-together-from-explode-not-just-one-how-to-do-it/#findComment-1118165 Share on other sites More sharing options...
tastro Posted October 1, 2010 Author Share Posted October 1, 2010 this works good but not perfect. from this: $pizza = "piece1 piece2 piece3 piece4"; $words = explode(' ', $pizza); $twoWords = array(); for($i=0, $wordCount=count($words); $i<$wordCount; $i+=2) { $twoWords[] = $words[$i] . ((isset($words[$i+1])) ? " {$words[$i+1]}" : '');foreach($twoWords as $wawa){echo $wawa.'<br />';} } i get this: piece1 piece2 piece1 piece2 piece3 piece4 also there is still one missing: piece1 piece4 Quote Link to comment https://forums.phpfreaks.com/topic/214952-need-to-get-2-words-together-from-explode-not-just-one-how-to-do-it/#findComment-1118167 Share on other sites More sharing options...
tastro Posted October 1, 2010 Author Share Posted October 1, 2010 and the first two are the same. :S also i need each value with each. Quote Link to comment https://forums.phpfreaks.com/topic/214952-need-to-get-2-words-together-from-explode-not-just-one-how-to-do-it/#findComment-1118168 Share on other sites More sharing options...
Psycho Posted October 1, 2010 Share Posted October 1, 2010 I misread your first post. I thought you just wanted the first two words, the second two words, etc. I just finished the regex solution for that. Oh well. Anyway, you apparently want each word paired with every other word, right? If so, your original example doesn't show piece1 with piece 3. Your results are different than what I had - you must have changed the code I gave you. Give me a minute to see what I can come up with. Quote Link to comment https://forums.phpfreaks.com/topic/214952-need-to-get-2-words-together-from-explode-not-just-one-how-to-do-it/#findComment-1118171 Share on other sites More sharing options...
Psycho Posted October 1, 2010 Share Posted October 1, 2010 Here you go: $pizza = "piece1 piece2 piece3 piece4"; $pieces = explode(' ', $pizza); $combinations = array(); for($x=0, $pieceCount=count($pieces); $x<$pieceCount; $x++) { for($y=$x+1; $y<$pieceCount; $y++) { $combinations[] = "{$pieces[$x]} {$pieces[$y]}"; } } print_r($combinations); Output: Array ( [0] => piece1 piece2 [1] => piece1 piece3 [2] => piece1 piece4 [3] => piece2 piece3 [4] => piece2 piece4 [5] => piece3 piece4 ) Quote Link to comment https://forums.phpfreaks.com/topic/214952-need-to-get-2-words-together-from-explode-not-just-one-how-to-do-it/#findComment-1118172 Share on other sites More sharing options...
tastro Posted October 2, 2010 Author Share Posted October 2, 2010 I misread your first post. I thought you just wanted the first two words, the second two words, etc. I just finished the regex solution for that. Oh well. Anyway, you apparently want each word paired with every other word, right? If so, your original example doesn't show piece1 with piece 3. Your results are different than what I had - you must have changed the code I gave you. Give me a minute to see what I can come up with. exactly mjdamato, i'm sry my bad, missed piece1 and piece3 :S also will try the new code now and i will report. Quote Link to comment https://forums.phpfreaks.com/topic/214952-need-to-get-2-words-together-from-explode-not-just-one-how-to-do-it/#findComment-1118176 Share on other sites More sharing options...
tastro Posted October 2, 2010 Author Share Posted October 2, 2010 awesomeeeeeee! thank you so much mate! now it works exactly like i wanted it to. i understand the whole code except this part: {$pieces[$x]} {$pieces[$y]} why are here { and } ? :S i normally do it like this: $pieces[$x].$pieces[$y] is there a difference? Quote Link to comment https://forums.phpfreaks.com/topic/214952-need-to-get-2-words-together-from-explode-not-just-one-how-to-do-it/#findComment-1118178 Share on other sites More sharing options...
tastro Posted October 2, 2010 Author Share Posted October 2, 2010 i have it like this now: $pizza = "piece1 piece2 piece3 piece4"; $pieces = explode(' ', $pizza); $combinations = array(); for($x=0, $pieceCount=count($pieces); $x<$pieceCount; $x++) { for($y=$x+1; $y<$pieceCount; $y++) { $combinations[] = ''.$pieces[$x].' '.$pieces[$y].''; } } print_r($combinations); and it seems that it's the same. but just wanted to know if { and } are any better in performance or just the everyones chooice? now after i see the code, i can't belive that i didn't came accross this. Quote Link to comment https://forums.phpfreaks.com/topic/214952-need-to-get-2-words-together-from-explode-not-just-one-how-to-do-it/#findComment-1118180 Share on other sites More sharing options...
Psycho Posted October 2, 2010 Share Posted October 2, 2010 $combinations[] = ''.$pieces[$x].' '.$pieces[$y].''; So, why would you concatentate empty strings at the beginning and end??? Why not just do this: $combinations[] = $pieces[$x].' '.$pieces[$y]; But to explain the {}, when using double quotes to define a string PHP will parse any variables in the string. It makes it much easier, in most instances, and much more readable to just include the variable in the string rather than exiting and entering out of quotes as you did. But, when including variables within double quotes there are times when a variable can be misinterpreted - such as if you want other characters butted up against the string value or with the case of array values using a key enclosed in single quotes. To prevent that you can enclose the variables in curly braces - {}. So, I make it a habit to always enclose my variables in curly braces. Quote Link to comment https://forums.phpfreaks.com/topic/214952-need-to-get-2-words-together-from-explode-not-just-one-how-to-do-it/#findComment-1118236 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.