Jump to content

need to get 2 words together from explode not just one, how to do it?


Recommended Posts

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

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]}" : '');
}

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

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.

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
)

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. ;)

awesomeeeeeee! thank you so much mate! :)  :o

 

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?

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. :D

 

      $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.

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.