【问题标题】:How to remove everything after a space in each of my array objects in PHP?如何在 PHP 中的每个数组对象中删除空格后的所有内容?
【发布时间】:2020-10-21 01:12:05
【问题描述】:

我有两个数组,每个数组对象都包含字母和数字。例如:“马修 20897”。我想从每个数组对象中删除数字,以便我可以比较两个数组并查看它们是否有任何共同的单词/名称。我尝试做一个爆炸来摆脱空间,但我得到一个错误,说爆炸()期望参数 2 是一个字符串。

下面是我目前的代码:

<?php
//simple read file and print
$boy = array();
$girl = array();
$connectionBoy = fopen("boynames.txt", "r") or die("Can't open boynames.txt file.");
$connectionGirl = fopen("girlnames.txt", "r") or die("Can't open girlnames.txt file.");

while(! feof($connectionBoy)){   //while it is not the end of the file
    
    $word = fgets($connectionBoy);  //read a record
    $word = rtrim($word); //gets rid of end of record char
    $boy[] = $word;
}

while(! feof($connectionGirl)){   //while it is not the end of the file
    
    $word2 = fgets($connectionGirl);  //read a record
    $word2 = rtrim($word2); //gets rid of end of record char
    $girl[] = $word2;
}

fclose($connectionBoy);
 echo "Number of names in the boynames file are ".sizeof($boy)."<br>";
 echo "Number of names in the boynames file are ".sizeof($girl);
 
 $itemBoy = explode(" ",$boy);
 $itemGirl = explode(" ",$girl);
 
 $result =array_intersect($itemBoy,$itemGirl);
 print_r($result);

另外,两个数组都存储了 1000 条记录,所以我必须删除两个数组中所有项目的空格之后的所有内容。

【问题讨论】:

    标签: php arrays string


    【解决方案1】:
    【解决方案2】:

    来自docs

    explode — 用一个字符串分割一个字符串

    explode 获取一个字符串并将其拆分为一个数组。 $boy 和 $girl 已经是数组了。

    如果我理解您要做什么,您可以通过以下方式摆脱循环中的数字:

    $splitWord2 = explode(' ', $word2);
    $girl[] = $splitWord2[0];
    

    【讨论】:

      【解决方案3】:

      从您的代码中,我看到您在错误的地方使用了explode 函数。它应该在while loop 中,因为字符串应该在那里展开以获得名称的第一个单词。我已经更新了你的代码。

      <?php
      
      $boy = array();
      $girl = array();
      $connectionBoy = fopen("boynames.txt", "r") or die("Can't open boynames.txt file.");
      $connectionGirl = fopen("girlnames.txt", "r") or die("Can't open girlnames.txt file.");
      
      while(! feof($connectionBoy)){   //while it is not the end of the file
          
          $line = fgets($connectionBoy);  //read a record
          $line = trim($line); //gets rid of end of record char
          $parts = explode(" ",$line);
          $boy[] = $parts[0];
      }
      
      while(! feof($connectionGirl)){   //while it is not the end of the file
          
          $line = fgets($connectionGirl);  //read a record
          $line = trim($line); //gets rid of end of record char
          $parts = explode(" ",$line);
          $girl[] = $parts[0];
      }
      
      fclose($connectionBoy);
      fclose($connectionGirl);
      
      echo "Number of names in the boynames file are ".sizeof($boy)."<br>";
      echo "Number of names in the boynames file are ".sizeof($girl);
      
      $result =array_intersect($boy,$girl);
      print_r($result);
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 2012-08-24
        • 2019-04-23
        • 2019-10-21
        • 2012-03-08
        • 2012-05-19
        • 2021-06-09
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        相关资源
        最近更新 更多