如何比较两个数组集合并显示在不同的选项卡中?

问题描述 投票:0回答:1

这是我的收集结果

//database data
    Collection {#1526 ▼
  #items: array:2 [▼
    0 => array:14 [▼
      "item_code" => "Albatrs001"
      "product_name" => "CHILLED SALMON WHOLE (CTNx2NOS/4-5KG FILLETS & Trimmings3KG) "
      "description" => "abc"
      "category" => "Seafood"
      "input_tax" => "ZP"
      "output_tax" => "ZRL"
      "sla" => 5.0
      "threshold_day" => 5.0
      "threshold_max_qty" => 0.0
      "order_limit" => 9.0
      "reference_qty" => null
      "reference_expiry_date" => "5"
      "default_unit_price" => 48.0
      "default_uom" => "KG"
    ]
    1 => array:14 [▼
      "item_code" => "Albatrs002"
      "product_name" => "SMOKED SALMON WHOLE /PKT "
      "description" => "cdf"
      "category" => "Seafood"
      "input_tax" => "TX"
      "output_tax" => "SR"
      "sla" => 5.0
      "threshold_day" => 5.0
      "threshold_max_qty" => 0.0
      "order_limit" => 4.0
      "reference_qty" => null
      "reference_expiry_date" => "5"
      "default_unit_price" => 80.0
      "default_uom" => "PKT"
    ]
  ]
}

//excel data


 Collection {#1526 ▼
  #items: array:3 [▼
    0 => array:14 [▼
      "item_code" => "Albatrs001"
      "product_name" => "CHILLED SALMON WHOLE (CTNx2NOS/4-5KG FILLETS & Trimmings3KG) "
      "description" => "abc"
      "category" => "Seafood"
      "input_tax" => "ZP"
      "output_tax" => "ZRL"
      "sla" => 5.0
      "threshold_day" => 5.0
      "threshold_max_qty" => 0.0
      "order_limit" => 9.0
      "reference_qty" => null
      "reference_expiry_date" => "5"
      "default_unit_price" => 48.0
      "default_uom" => "KG"
    ]
    1 => array:14 [▼
      "item_code" => "Albatrs002"
      "product_name" => "SMOKED SALMON WHOLE /BAG "
      "description" => "ggg"
      "category" => "Seafood"
      "input_tax" => "TX"
      "output_tax" => "SR"
      "sla" => 5.0
      "threshold_day" => 5.0
      "threshold_max_qty" => 0.0
      "order_limit" => 4.0
      "reference_qty" => null
      "reference_expiry_date" => "5"
      "default_unit_price" => 80.0
      "default_uom" => "PKT"
    ]
    2 => array:14 [▼
          "item_code" => "Albatrs003"
          "product_name" => "ABCDEFG "
          "description" => "cccc"
          "category" => "dddd"
          "input_tax" => "TX"
          "output_tax" => "SR"
          "sla" => 5.0
          "threshold_day" => 5.0
          "threshold_max_qty" => 0.0
          "order_limit" => 4.0
          "reference_qty" => null
          "reference_expiry_date" => "5"
          "default_unit_price" => 80.0
          "default_uom" => "PKT"
    ]
  ]
}

我想比较两个集合并在选项卡中显示新记录和更改。我有两个选项卡来显示新记录和更改记录。我只会显示新的并记录所做的更改。

我正在使用嵌套循环来比较和突出显示更改,但是有没有任何方法可以仅显示新的并记录所做的更改并将它们分配给不同的选项卡?

// want to compare
$reader = \Excel::load(Input::file('import_file'))->toArray(); 
$result = DB::table('items')->select('....')->get();
php arrays laravel collections
1个回答
2
投票

基于给定数组的工作解决方案:

$db = array_map('serialize', $db);
$excel = array_map('serialize', $excel);
$diff = array_map('unserialize', array_diff($excel, $db));

因此,您序列化数组。您将获得一个字符串数组(序列化数组)。然后使用

array_diff
来比较这些字符串。然后你只需反序列化结果即可。最后,你得到一个数组。

© www.soinside.com 2019 - 2024. All rights reserved.