开发者

php array manipulation help

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 = a开发者_StackOverflowrray('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');

I hope my question is clear.


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
        )

)


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜