I have 2 files (A.txt and B.txt). I want to change / update file B of information on file A. the contents of the file A is :
我有2个文件(A.txt和B.txt)。我想更改/更新文件A上的信息文件B.文件A的内容是:
Fish 2 4
Horse 3 2
Chicken 1 5
the contents of the file B is :
文件B的内容是:
not 5 2
not 3 2
not 2 2
not 2 4
I want to get his results from file B, like this:
我想从文件B中得到他的结果,如下所示:
not 5 2
Horse 3 2
not 2 2
Fish 2 4
I know it use the expression "if". but I do not know how to code. is there anything that can help? in the file are separated by the "tabs" and "newline"
我知道它使用“if”这个表达式。但我不知道如何编码。有什么可以帮助吗?在文件中由“制表符”和“换行符”分隔
0
Try
<?php
foreach (file("A.txt") as $line)
{
// Split line
$field = preg_split('/\s+/',$line);
// This is your array index
$index = $field[1].' '.$field[2];
// Array holds name which is first field
$Array[$index] = $field[0];
}
foreach(file("B.txt") as $line)
{
// Split field
$field = preg_split('/\s+/',$line);
// 2nd file index
$index = $field[1].' '.$field[2];
// if index is set in array echo name else line
echo (isset($Array[$index])?$Array[$index].' '.$index:$line).'<br>';
}
?>
Would result for given input
将给出输入的结果
not 5 2
Horse 3 2
not 2 2
Fish 2 4
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2014/04/05/d05b265626dbeeed99cd2fbd53233146.html。