【发布时间】: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 条记录,所以我必须删除两个数组中所有项目的空格之后的所有内容。
【问题讨论】: