在数组中循环时使用PHP
各位,我在正确嵌套while循环读取2个数组时遇到了问题。 我有2个文件,我从中读取内容:在数组中循环时使用PHP,php,arrays,loops,Php,Arrays,Loops,各位,我在正确嵌套while循环读取2个数组时遇到了问题。 我有2个文件,我从中读取内容: file1: item_list.txt string1 \n string2 \n string3 \n ... file2: item+info.txt string3 \t info1 \t info2 \t info3 string1 \t info7 \t info1 \t info4 string5 \t info2 \t info3 string2 \t info2 \t info4 \t
file1: item_list.txt
string1 \n
string2 \n
string3 \n
...
file2: item+info.txt
string3 \t info1 \t info2 \t info3
string1 \t info7 \t info1 \t info4
string5 \t info2 \t info3
string2 \t info2 \t info4 \t info1
(值仅由新行和制表符分隔,我在字符之间添加了一个空格以增加可读性)
我使用fgetcsv()函数读取文件,文件中的每一行作为数组存储到变量$data中。我使用条件(!feof($fp))创建了一个while循环,以读取文件直到最后一行。但是我不能很好地嵌套第二个循环
我想用这个做什么:
读取文件1中找到的第一个字符串,转到文件2并尝试查找该字符串。如果存在匹配项,则获取该字符串的信息数据(所有数据,或仅一个数据,都无关紧要)。如果不匹配,则返回消息“不匹配”。在这两种情况下,一旦第二个循环完成,我需要读取file1中的第二个字符串,然后再次在file2中进行搜索。只要文件1中有要读取的内容,就重复此操作
这里有两个版本的代码,它们不起作用,我不知道为什么
//opening the files
$fp = fopen("$DOCUMENT_ROOT/test/item_list.txt", "r"); #raw item list
$pf = fopen("$DOCUMENT_ROOT/test/item+info.txt", "r"); #item+info list
//read from first file
$line=0;
while (!feof($fp)){
$line++;
$data1 = fgetcsv($fp, "1000", "\n");
$item1= $data1[0];
echo "line: $line. item_list: ".$item1; //just to see on screen what's happening
print_r($data1); //same here, just to see what's going on
echo"<br />";
//searching for string in file2
$row=0;
while (!feof($pf)){
$row++;
$data2 = fgetcsv($pf, "1000", "\t");
$item2= $data2[0];
echo "line: $row. item+info: ".$item2; //just checking things on screen
print_r($data2); //here too
echo "<br />";
//conditioning
//equal strings
if ($string1== $string2)
echo $data2[1]."<br />";
break;
}
}
fclose($fp);
fclose($pf);
这是一个粗略的尝试:
$filename1 = 'item_list.txt';
$filename2 = 'item+info.txt';
# Init the Raw Array
$content2raw = array();
# Get the File Contents for File 2
$file2 = file_get_contents( $filename2 );
# Split it into an Array by Line Breaks
$content2raw = preg_split( "/\n/" , $file2 , -1 , PREG_SPLIT_NO_EMPTY );
# Unset the variable holding the file contents
unset( $file2 );
# Init the Fixed Array
$content2 = array();
# Loop through the Raw Array
foreach( $content2raw as $l ){
// Each Line of Filename2
# Split the Line on Tabs
$t = preg_split( "/\s*\t\s*/" , $l , -1 );
# Set the Fixed Array, using the first element from the line as the key
$content2[ $t[0] ] = $t;
}
# Unset the Raw Array
unset( $content2raw );
# Get the File Contents from File 1
$file1 = file_get_contents( $filename1 );
# Split it into an Array by Line Breaks
$contents1 = preg_split( "/\n/" , $file1 , -1 , PREG_SPLIT_NO_EMPTY );
# Unset the variable holding the file contents
unset( $file1 );
# Loop through the Lines, using each line as the Key to look for
foreach( $content1 as $v ){
# Check whether a matching element exists in the array from File 2
if( !array_key_exists( $k , $content2 ) ){
// No Match Found
echo 'No Match';
}else{
// Match Found
echo 'Match Found';
var_dump( $content2[$v] );
}
}
根据@Bluewind的意见/反馈进行修订
这实际上比你想象的要少得多。首先,您读取一个信息文件并用它构建一个哈希表:
foreach(file("info_list") as $line) {
$line = explode("\t", trim($line));
$info[$line[0]] = $line;
}
然后遍历items文件,查看散列中是否有匹配的条目:
foreach(file("item_list") as $line) {
$item = trim($line);
if(isset($info[$item]))
// we have some info for this item
else
// we have no info for this item
}
基本上就是这样的这应该可以做到:
$file1 = file($DOCUMENT_ROOT . '/test/item_list.txt');
$file2 = file($DOCUMENT_ROOT . '/test/item+info.txt');
foreach ($file1 as $line)
{
$line = rtrim($line); // just in case ...
if ($line === '') continue;
foreach($file2 as $infoline)
{
$infoline = explode("\t", rtrim($infoline);
if ($line === $infoline[0])
{
array_shift($infoline);
echo $line . '<br /><br />' . implode('<br />', $infoline);
// $results[$line] = $infoline; // uncomment this if you need the search results stored for later use
break;
}
}
}
$file1=file($DOCUMENT_ROOT./test/item_list.txt');
$file2=file($DOCUMENT_ROOT.'/test/item+info.txt');
foreach($file1作为$line)
{
$line=rtrim($line);//以防万一。。。
如果($line=='')继续;
foreach($file2作为$infoline)
{
$infoline=explode(“\t”),rtrim($infoline);
如果($line==$infoline[0])
{
阵列移位($infoline);
echo$line.'
'。内爆('
',$infoline);
//$results[$line]=$infoline;//如果需要存储搜索结果以供以后使用,请取消对此的注释
打破
}
}
}
您希望的输出是什么,以及您从每个脚本获得的输出是什么。(对于第二个脚本,如果右括号缩进,这将更容易阅读。)@Micahel first script只输出一个匹配项,但它应该为file1中的每个项输出匹配项,因为我在两个文件中都有相同的项(用于测试目的),但顺序不同。第二个脚本输出了一些随机字母。我确信它们根本不是随机的,只是我无法确定从哪个字符串的哪一部分获取并合并到输出中。很抱歉,没有缩进右括号。您应该使用file()要将文件读入数组而不是文件,请获取内容并预拆分。每天学习新内容…为建议干杯。
foreach(file("item_list") as $line) {
$item = trim($line);
if(isset($info[$item]))
// we have some info for this item
else
// we have no info for this item
}
$file1 = file($DOCUMENT_ROOT . '/test/item_list.txt');
$file2 = file($DOCUMENT_ROOT . '/test/item+info.txt');
foreach ($file1 as $line)
{
$line = rtrim($line); // just in case ...
if ($line === '') continue;
foreach($file2 as $infoline)
{
$infoline = explode("\t", rtrim($infoline);
if ($line === $infoline[0])
{
array_shift($infoline);
echo $line . '<br /><br />' . implode('<br />', $infoline);
// $results[$line] = $infoline; // uncomment this if you need the search results stored for later use
break;
}
}
}