在字符串数组中使用字数统计功能

时间:2015-03-15 07:54:49

标签: php arrays

我的代码如下。

1)我需要获取消息数组中每条消息中的单词数,并将其显示在一个新数组中

2)如果每条消息中的单词数小于5,我应该将单词“less”添加到另一个新数组中,如果每条消息中的单词数大于5,我应该添加字符串“更大”到同一个新数组。

我不确定如何将这个“更大”和“更小”放入一个新阵列。请告知。(参见问题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";
    }
}

3 个答案:

答案 0 :(得分:1)

首先,在这种情况下,您可以使用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.

答案 1 :(得分:0)

试试这个:它对我来说很好:

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

答案 2 :(得分:0)

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

$numberOfMessages = count($messages);

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

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

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

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

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

你制作的第二个新阵列是一种类似的方式。

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

这一次,我们从新的$word数组中获取每个值($words)并检查它的大小,并填充第二个新数组$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>';