比较及;用PHP编辑两个制表符文件?

比较及;用PHP编辑两个制表符文件?,php,arrays,file,compare,tab-delimited,Php,Arrays,File,Compare,Tab Delimited,我想比较两个tabdelimetered文件。我将文件转换为两个具有以下结构的数组: 数组1 Array ( [0] => Array ( [name] => name1 [qty] => 200 ) [1] => Array ( [name] => name

我想比较两个
tabdelimetered
文件。我将文件转换为两个具有以下结构的数组:

数组1

Array
    (
        [0] => Array
            (
                [name] => name1
                [qty] => 200
            )

        [1] => Array
            (
                [name] => name2
                [qty] => 9
            )

        [2] => Array
            (
                [name] => name3
                [qty] => 3
            )

        [3] => Array
            (
                [name] => name4
                [qty] => 1
            )
    )
阵列2

Array
    (
        [0] => Array
            (
                [name] => name1
                [qty] => 180
            )

        [1] => Array
            (
                [name] => name2
                [qty] => 9
            )

    )

如何比较这两个数组以及值不同的位置,以将数组2中的值替换为值为1的数组。

最简单的方法是为第二组数据创建关联数组,而不是上面使用的数组格式。因为您似乎只有两个“列”,而且它们实际上是一个键/值关系,所以这应该是好的和简单的

本例使用您生成的两个输入数组执行此操作,但您可能可以对此进行调整,以便在读取第二个数组时直接创建关联数组:

 // first create an associative array from the second indexed array
 $secondAssoc = array();
 foreach ($secondArray as $row) {
   $secondAssoc[$row['name']] = $row['qty'];
 }
 /*
  $secondAssoc now looks like:
  Array
      (
          [name1] => 180
          [name2] => 9
      )
 */

 // Now loop the first array and update it
 foreach ($firstArray as $rowId => $row) {
   if (isset($secondAssoc[$row['name']]) && $secondAssoc[$row['name']] != $row['qty']) {
     $firstArray[$rowId]['qty'] = $secondAssoc[$row['name']];
   }
 }
 /*
  $firstArray now looks like this:
  Array
      (
          [0] => Array
              (
                  [name] => name1
                  [qty] => 180
              )
          [1] => Array
              (
                  [name] => name2
                  [qty] => 9
              )
          [2] => Array
              (
                  [name] => name3
                  [qty] => 3
              )
          [3] => Array
              (
                  [name] => name4
                  [qty] => 1
              )
      )
  */

编辑这是一个版本,它还创建了一个数组,
$modifiedItems
,该数组只保存已更改的项:

 // first create an associative array from the second indexed array
 $secondAssoc = array();
 foreach ($secondArray as $row) {
   $secondAssoc[$row['name']] = $row['qty'];
 }

 // Now loop the first array and update it
 $modifiedItems = array();
 foreach ($firstArray as $rowId => $row) {
   if (isset($secondAssoc[$row['name']]) && $secondAssoc[$row['name']] != $row['qty']) {
     $firstArray[$rowId]['qty'] = $secondAssoc[$row['name']];
     $modifiedItems[] = array('name'=>$row['name'],'qty'=>$secondAssoc[$row['name']]);
   }
 }

!它的工作原理不同,因为两个数组都是关联数组的索引数组。我的代码在更新之前将一个数组转换为一个一维关联数组,这使它能够工作。谢谢!我理解!我还有一个问题。如何从$firstArray和编辑$firstArray生成具有不同值的数组。提前谢谢@dilyan_kn抱歉,我不太明白你想要什么,你的意思是你想要生成一个只保存已更新值的数组?是的。我想生成一个数组,该数组只保存已更新的值