在字符串数组中使用字数统计函数


Using word count function in string arrays

我的代码如下。

1)我需要获取消息数组中每条消息中的字数并将其显示在新数组中

2)如果每条消息中的单词数小于5,我应该将单词"small"添加到另一个新数组

中,如果每条消息中的单词数大于5,我应该将字符串"bigger"添加到同一个新数组中。

我不确定如何将这个"更大"和"更小"放入新数组中。请指教。(请参阅问题 2)

$messages = array(
    "Learning PHP is fun",
    "I want to learn more",
    "Girls can code too",
    "Goodbye boredom!",
    "Coding Geek",
    "Computers can sometimes freeze up",
    "Follow me now", "Coding is cool",
    "Computer nerds are for real",
    "This is the end of all the messages"
);
/* (1) */ 
for ($i = 0; $i < 10; $i++) {
    $words[] = str_word_count($messages[$i]);
}
print_r($words);
echo "<br>";
/* (2) */
for ($i = 0; $i < 10; $i++) {
    if ($words[$i] <= 5) {
        $size[] = $words[$i];
        echo "smaller" . "<br>";
    } else {
        $size[] = $words[$i];
        echo"bigger";
    }
}

首先,在这种情况下,您可以使用foreach而不是for循环。

其次,这段代码就足够了。试试吧 :

foreach($messages as $val){
    $len = str_word_count($val);
    if($len<=5){
        $msg =  "bigger";
    }else{
        $msg =  "smaller";
    }
    $size[] = array("size"=>$len, "msg"=>$msg);
}
# Your $size array would be like :
# array( 
#          array("size"=>3, "msg"=>"smaller"),
#          array("size"=>8, "msg"=>"bigger") 
#    )
# And you can print it or use it everywhere.

for loops 适用于数组。您可以考虑使用一个变量来保存要在循环中使用的原始数组的长度。

$numberOfMessages = count($messages);
$for ($i = 0; $i < $numberOfMessages; $i ++) {
...
}

我的首选方法与使用 foreach 循环的前两个示例相同。

foreach ($messages as $message) {
    $words[] = str_word_count($message);
}

这将通过计算原始数组($messages)中每个值($message)中的单词数来创建您的第一个名为$words的新数组。使用 <pre> 标记可格式化回显并使其更具可读性。

echo '<pre>' . print_r($words, true) . '</pre>';

您的第二个新数组的制作方式类似。

foreach ($words as $word) {
    if ($word < 5) {
        $size[] = 'smaller';
    } else {
        $size[] = 'bigger';
    }
}

这次我们从新的$words数组中获取每个值($word),并检查它的大小并填充第二个新数组$size。 使用三元运算符的 bablu 示例 ?: 此示例只是编写相同条件语句的长手方法。

将一切整合在一起

$messages = array (
    'Learning PHP is fun',
    'I want to learn more',
    'Girls can code too',
    'Goodbye boredom!',
    'Coding Geek',
    'Computers can sometimes freeze up',
    'Follow me now', 'Coding is cool',
    'Computer nerds are for real',
    'This is the end of all the messages'
);
foreach ($messages as $message) {
    $words[] = str_word_count($message);
}
echo '<pre>' . print_r($words, true) . '</pre>';
foreach ($words as $word) {
    if ($word < 5) {
        $size[] = 'smaller';
    } else {
        $size[] = 'bigger';
    }
}
echo '<pre>' . print_r($size, true) . '</pre>';

试试这个:它对我来说工作正常:

<?php
$messages=array
("Learning PHP is fun","I want to learn more","Girls can code too","Goodbye boredom!","Coding Geek","Computers can sometimes freeze up","Follow me now","Coding is cool","Computer nerds are for real","This is the end of all the messages");
$newMessageArray = array();
$newCountArray = array();
foreach($messages as $message) {
    $length = str_word_count($message);
    $newMessageArray[] = $message;
    $newCountArray[] = ($length >=5)?'Bigger':'Smaller';
}
    echo '<pre>'; print_r($newMessageArray);
    echo '<pre>'; print_r($newCountArray);
?>