简体   繁体   English

php数组操作帮助

[英]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 = 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');

I hope my question is clear. 希望我的问题清楚。

Instead of $phrase2, $phrase3, ... my code uses $phrase[2], $phrase[3], ... 代替$ phrase2,$ phrase3,...我的代码使用$ 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,$ phrase3,$ phrase4,则只需在我的末尾添加以下代码即可:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM