Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从数组中排除数字_Php - Fatal编程技术网

Php 从数组中排除数字

Php 从数组中排除数字,php,Php,下面的代码返回一个表,其中包含$commentstring中显示的每个单词或数字的行。每个单词或数字在下表中显示为$word。如何排除数字 $words = explode(" ", $commentstring); $result = array_combine($words, array_fill(0, count($words), 0)); arsort($words); foreach($words as $word) { $result[$w

下面的代码返回一个表,其中包含
$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>";
$words=explode(“,$commentstring);
$result=array_combine($words,array_fill(0,count($words),0));
阿尔索特(大写);
foreach($words作为$word){
$result[$word]++;
阿尔索特(结果);
}
回声“;
foreach($word=>$count1的结果){
回声';
回声';
回显“$word”;
回声';
回声';
回显“$count1”;
回声';
回声';
}
回声“;

您可以使用
is\u numeric
检查每个
$word
是否为数字,如果不是,则只将其插入数组中

if (!is_numeric($word)) {
    if (!isset($result[$word]))
        $result[$word] = 0;
    $result[$word]++;
    arsort($result);
}

编辑:另外,您真的需要在每个增量上对数组进行排序吗?为什么不在末尾对其进行排序呢?

您可以使用
is\u numeric
来检查每个
$word
是否为数字,如果不是,则只将其插入数组中

if (!is_numeric($word)) {
    if (!isset($result[$word]))
        $result[$word] = 0;
    $result[$word]++;
    arsort($result);
}

编辑:另外,您真的需要在每个增量上对数组进行排序吗?为什么不在最后对其进行排序?

如果我正确理解您的问题,您可以使用
is\u numeric()
函数检查$word var是否是一个数字

foreach($result as $word => $count1) {
    if(is_numeric($word)) { continue; }
    ...

如果我正确理解您的问题,您可以使用
is\u numeric()
函数检查$word var是否是一个数字

foreach($result as $word => $count1) {
    if(is_numeric($word)) { continue; }
    ...

这似乎是一种工作。它将所有数字放在表的底部,在
$count1
中,所有数字都为零。我怎样才能从表中删除这些数字?哦,我知道问题出在哪里。您正在预填充阵列。只需创建数组而不将其设置为array_combine值,就可以了。只需替换
$result=array_combine($words,array_fill(0,count($words),0))$result=array()的code>你哪里都不会有。当您在结果数组中迭代时,您会立即填充$words数组。如果您不愿意相信php在尚未设置
$result[$word]
值的情况下正确设置该值,您可以添加
如果(!isset($result[$word]))$result[$word]=0
到我的注释中的if语句。我用这个编辑了我的答案。这似乎是一种工作。它将所有数字放在表的底部,在
$count1
中,所有数字都为零。我怎样才能从表中删除这些数字?哦,我知道问题出在哪里。您正在预填充阵列。只需创建数组而不将其设置为array_combine值,就可以了。只需替换
$result=array_combine($words,array_fill(0,count($words),0))$result=array()的code>你哪里都不会有。当您在结果数组中迭代时,您会立即填充$words数组。如果您不愿意相信php在尚未设置
$result[$word]
值的情况下正确设置该值,您可以添加
如果(!isset($result[$word]))$result[$word]=0
到我的注释中的if语句。我用这个编辑了我的答案。