Yalamber
Yalamber

Reputation: 7580

Use a flat array of single words to create new arrays with increasing numbers of words per array element

I have an array with words below as an example.

$words = array('hello','world','wad','up','yo','etc');

What I want is next array with following by manipulating above

$phrase2 = array('hello world', 'world wad', 'wad up', 'up  yo', 'yo etc');
$phrase3 = array('hello world wad', 'world wad up', 'wad up yo', 'up  yo etc');
$phrase4 = array('hello world wad up', 'world wad up yo', 'wad up yo etc');
$phrase5 = array('hello world wad up yo', 'world wad up yo etc');

Upvotes: 0

Views: 109

Answers (3)

kamawanu
kamawanu

Reputation: 11

I tried this for my programming exercise.

<?php

$src1 = "hello world wad up yo etc";
$words = explode(" ",$src1);
$length = count($words);
print_r($words);

$phrases = array();

foreach( range(2,5) as $span ){
    $shifter = create_function("\$n","{ global \$words; return join(\" \", array_slice(\$words,\$n,$span) ); }");
    $sp = array_map( $shifter, range( 0, $length - $span ) );
    ###var_dump($sp);
    $phrases["phrase$span"] = $sp;
}

print_r($phrases);

Upvotes: 1

ahmet2106
ahmet2106

Reputation: 5007

Instead of $phrase2, $phrase3, ... my code uses $phrase[2], $phrase[3], ...

But if you want to use $phrase2, $phrase3, $phrase4, then just add the following code to the end of mine:

$phrase2 = $phrase[2];
$phrase3 = $phrase[3];
$phrase4 = $phrase[4];
$phrase5 = $phrase[5];

Here my code, just try it!:

<?php
$words = array('hello','world','wad','up','yo','etc');
$phrase = array();
for($i=2; $i<=count($words); $i++)
{
    foreach($words as $key => $value)
    {
        if($key <= count($words)-$i)
        {
            $phrase_value[] = $value;

            for($j=1; $j<$i; $j++)
            {
                $phrase_value[] = $words[$key+$j];
            }

            $phrase[$i][] = implode(' ', $phrase_value);
            unset($phrase_value);
        }
    }
}
// this deletes the last array in phrase
array_pop($phrase);
?>

And the output looks like this:

Array
(
    [2] => Array
        (
            [0] => hello world
            [1] => world wad
            [2] => wad up
            [3] => up yo
            [4] => yo etc
        )

    [3] => Array
        (
            [0] => hello world wad
            [1] => world wad up
            [2] => wad up yo
            [3] => up yo etc
        )

    [4] => Array
        (
            [0] => hello world wad up
            [1] => world wad up yo
            [2] => wad up yo etc
        )

    [5] => Array
        (
            [0] => hello world wad up yo
            [1] => world wad up yo etc
        )

    [6] => Array
        (
            [0] => hello world wad up yo etc
        )

)

Upvotes: 3

Niko Efimov
Niko Efimov

Reputation: 2213

Slice array and then implode the results:

$words = array('hello','world','wad','up','yo','etc');
$phrase2 = array();
$phrase3 = array();
$phrase4 = array();
$lim = sizeof($words);
for($i=0;$i<$lim;$i++)
{
    if($i < $lim - 1)
        $phrase2[] = implode(" ",array_slice($words,$i,2)); 
    if($i < $lim - 2)
        $phrase3[] = implode(" ",array_slice($words,$i,3)); 
    if($i < $lim - 3)
        $phrase4[] = implode(" ",array_slice($words,$i,4)); 
}

Upvotes: 2

Related Questions