Reputation: 867
This is my collection result
//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"
]
]
}
I want to compare both collection and show the new record and changes in tab. I have two tabs to show new and changes record. I will only show new and record that made changes.
I am using nested loop to compare and highlight the changes, but is there any method to show only new and record that made changes and assign them to different tab?
// want to compare
$reader = \Excel::load(Input::file('import_file'))->toArray();
$result = DB::table('items')->select('....')->get();
Upvotes: 2
Views: 4840
Reputation: 163768
The working solution based on given arrays:
$db = array_map('serialize', $db);
$excel = array_map('serialize', $excel);
$diff = array_map('unserialize', array_diff($excel, $db));
So, you serialize arrays. You get an array of strings (serialized arrays). Then you use array_diff
to compare these strings. Then you just unserialize results. In the end, you're getting an array.
Upvotes: 2