There's a lot of questions out there that deals between arrays and multi-dimensional arrays but only one so how about two then? Let's say this is the data:
Array
(
[0] => Array
(
[BRANCHCODE] => 2
[BRANCH] => BRANCH 1
[AGREEID] => 1
)
[1] => Array
(
[BRANCHCODE] => 2
[BRANCH] => BRANCH 2
[AGREEID] => 2
)
[2] => Array
(
[BRANCHCODE] => 2
[BRANCH] => BRANCH 3
[AGREEID] => 3
)
)
Array
(
[0] => Array
(
[BRANCHCODE] => 2
[BRANCH] => BRANCH 4
[AGREEID] => 1
)
[1] => Array
(
[BRANCHCODE] => 2
[BRANCH] => BRANCH 5
[AGREEID] => 4
)
[2] => Array
(
[BRANCHCODE] => 2
[BRANCH] => BRANCH 6
[AGREEID] => 5
)
)
I managed to get the duplicated data but I can't get the unique data. Here's the php code on how I got the duplicates and also the count of the duplicated data.
In here I loop through both of the two arrays the first one is the array of the uploaded data and the next loop is the database results. I compared the two arrays if the AGREEID in the uploaded data has duplicate in the database. If the AGREEID in the uploaded data is unique I will insert it in the database.
foreach ($result as $key=>$upload_data) {
$agreeid_upload = $result[$key]['AGREEID'];
$data = $result[$key];
$another_data = $result[$key];
foreach ($reports as $dbase_data) {
$agreeid = $dbase_data->AGREEID;
if($agreeid_upload == $agreeid){ /// record has duplicate in the database
$count_duplicates = $count_duplicates + 1;
$duplicates[$key] = $data;
}else{
///here i want to store into another array the unique data..
}
}
}
答案 0 :(得分:1)
The method used would work, but is going to be inefficient with a large amount of data because it will do a lot of extra looping.
$dbAgreeeData = array();
$duplicates = array();
$unique = array();
foreach ($reports as $dbase_data) {
$dbAgreeeData[$db_dbase_data->AGREEID] = $dbase_data;
}
foreach ($result as $key=>$upload_data) {
$agreeId = $upload_data['AGREEID'];
if (isset($dbAgreeeData[$agreeId)){
$duplicates[$agreeId] = $upload_data;
}
else {
$unique[$agreeId] = $upload_data;
}
}
$numDuplicate = count($duplicates);
$numUnique = count($unique);
Or
$dbAgreeeData = array();
$uploadData= array();
foreach ($reports as $dbase_data) {
$dbAgreeeData[$db_dbase_data->AGREEID] = $dbase_data;
}
foreach ($result as $key=>$upload_data) {
$uploadData[$upload_data['AGREEID']] = $upload_data;
}
$duplicates = array_intersect_key($dbAgreeData, $uploadData;
$unique = array($dbAgreeData, $uploadData);
$numDuplicate = count($duplicates);
$numUnique = count($unique);