[英]Excluding numbers from an array
下面的代码返回一个表,该表在$commentstring
中出现的每个单词或数字都有一行。 下表中的每个单词或数字都显示为$word
。 如何排除数字?
$words = explode(" ", $commentstring);
$result = array_combine($words, array_fill(0, count($words), 0));
arsort($words);
foreach($words as $word) {
$result[$word]++;
arsort($result);
}
echo "<table>";
foreach($result as $word => $count1) {
echo '<tr>';
echo '<td>';
echo "$word";
echo '</td>';
echo '<td>';
echo "$count1 ";
echo '</td>';
echo '</tr>';
}
echo "</table>";
您可以使用is_numeric
来检查每个$word
是否为数字,如果不是,则仅将其插入到数组中。
if (!is_numeric($word)) {
if (!isset($result[$word]))
$result[$word] = 0;
$result[$word]++;
arsort($result);
}
编辑:另外,您是否真的需要在每个增量上对数组进行排序? 为什么不只在末尾排序呢?
如果我理解正确的问题,可以使用is_numeric()
函数检查$ word var是否为数字
foreach($result as $word => $count1) {
if(is_numeric($word)) { continue; }
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.