PHP foreach()循环

PHP foreach()循环,php,Php,我在for循环中分配并初始化了数组,例如array1,array2…arrayN,我是这样做的 $string = "The complete archive of The New York Times can now be searched from NYTimes.com " //the actual input is unknown, it would be read from textarea $size = the longest word length from the str

我在for循环中分配并初始化了数组,例如array1,array2…arrayN,我是这样做的

$string = "The complete archive of The New York Times can now be searched from NYTimes.com " //the actual input is unknown, it would be read from textarea  

$size = the longest word length from the string 

因此,我的问题是如何在simple for loop或foreach loop中为$array1、$array2、$array3……分配$string值,因为输入文本或最长单词的大小未知

我可能会从
$words=explode('',$string)
然后按字长对字符串排序

$array1 = [""];
$array2 = ["of", "be", ...]
$array3 = ["the", "can", "now", ...] and so on 
通过循环单词,你不需要知道单词的最大长度

最终的解决方案非常简单

$sortedWords = array(
    1 => array('a', 'I'),
    2 => array('to', 'be', 'or', 'is'),
    3 => array('not', 'the'),
);

您可以使用以下内容:

foreach ($words as $word) {
    $wordLength = strlen($word);
    $sortedWords[ $wordLength ][] = $word;
}
这将
$string
拆分为一个
$words
数组,然后计算每个单词的长度,并将该单词推送到相应的数组。

您可以使用explode()。
$words = explode(" ", $string);
foreach ($words as $w) {
    array_push(${"array" . strlen($w)}, $w);
}
$string=“现在可以从NYTimes.com搜索《纽约时报》的完整档案”; $arr=分解(“,$string); $count=计数($arr); $big=0; 对于($i=0;$i<$count;$i++){ $p=strlen($arr[$i]);
如果($big只需使用单词长度作为索引,并在每个单词后面添加
[]

you can use explode().

$string = "The complete archive of The New York Times can now be searched           from NYTimes.com " ;

$arr=explode(" ",$string);
$count=count($arr);

$big=0;
 for ($i = 0; $i < $count; $i++) {
     $p=strlen($arr[$i]);
if($big<$p){ $big_val=$arr[$i]; $big=$p;}

     }
echo $big_val;
要删除重复项,
$array=array\u map('array\u unique',$array);

收益率:

foreach(explode(' ', $string) as $word) {
    $array[strlen($word)][] = $word;
}

如果要重新索引主数组,请使用
array\u values()
,如果要重新索引子数组,请使用
array\u map()
array\u values()

我不太明白您的要求,但您似乎不明白数组是如何工作的。既然您想要一个每个元素都是文本的数组,只需执行:$array[0]=“”;,$array[1]=“of,be…”等等(在您的示例中,您已经定义了许多数组,但只是将它们用作字符串!)您放弃了还是什么???获取未定义的变量:array1当我尝试打印$array1,$array2…等等,这不是数组的工作方式。请尝试类似这样的操作:
foreach($array as$len=>$list){echo“$len:”。内爆(',',$list)。“
\n”}
如果需要长度为3的单词,则
回显内爆(',',$array[3]);
最终解决方案看起来非常熟悉。
you can use explode().

$string = "The complete archive of The New York Times can now be searched           from NYTimes.com " ;

$arr=explode(" ",$string);
$count=count($arr);

$big=0;
 for ($i = 0; $i < $count; $i++) {
     $p=strlen($arr[$i]);
if($big<$p){ $big_val=$arr[$i]; $big=$p;}

     }
echo $big_val;
foreach(explode(' ', $string) as $word) {
    $array[strlen($word)][] = $word;
}
Array
(
    [3] => Array
        (
            [0] => The
            [2] => New
            [3] => can
            [4] => now
        )

    [8] => Array
        (
            [0] => complete
            [1] => searched
        )

    [7] => Array
        (
            [0] => archive
        )

    [2] => Array
        (
            [0] => of
            [1] => be
        )

    [4] => Array
        (
            [0] => York
        )

    [5] => Array
        (
            [0] => Times
        )
)