Domanda

I want to compare two tabdelimeted files. I take the files and converts them into two arrays with the following structure:

Array 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
            )
    )

Array 2

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

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

    )

How can I compare these two arrays and where the value is different to replace the value in the array 2 with array of value 1.

È stato utile?

Soluzione

The easiest way to do this would be to create an associative array for the second set of data, instead of the array format you has used above. Since you only seem to have two "columns", and these are effectively a key/value relationship this should be nice and easy.

This example takes the two input arrays you have generated to do it, but you can probably adjust this so that you create the associative array directly as you read the second:

 // 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
              )
      )
  */

See it working.

EDIT Here is a version that also creates an array, $modifiedItems, that holds only the items that have changed:

 // 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']]);
   }
 }

See it working.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top