MariaJen
MariaJen

Reputation: 1143

Collect duplicate data in array using php

this is my sample array data from bio-metrics I just want to collect data that has the same bio_id and date

temp:[
0:{
 bio_id:"1"
 date:"2017-10-05"
 date_time:"2017-10-05 08:00:22"
 device_name:"biometrics"
 time:"08:00:22"
}
1:{
 bio_id:"1"
 date:"2017-10-05"
 date_time:"2017-10-05 08:00:23"
 device_name:"biometrics"
 time:"08:00:23"
}
2:{
 bio_id:"2"
 date:"2017-10-05"
 date_time:"2017-10-05 08:06:29"
 device_name:"biometrics"
 time:"08:06:29"
}
3:{
 bio_id:"1"
 date:"2017-10-05"
 date_time:"2017-10-05 15:06:47"
 device_name:"biometrics"
 time:"15:06:47"
}
4:{
 bio_id:"2"
 date:"2017-10-05"
 date_time:"2017-10-05 16:01:50"
 device_name:"biometrics"
 time:"16:01:50"
}
] 

I been stuck with this code that I made, and don't know how I should manipulate it, or how I will store it properly, I have try some array function but it gives different result to my data

$len = count($temp);
for ($i=0; $i <$len ; $i++) { 
  $id = $temp[$i]['bio_id'];
  $date = $temp[$i]['date'];
 for ($x=0; $x < $len; $x++) { 
   if ($id == $temp[$x]['bio_id'] && $date == $temp[$x]['date']) {
         $data[] = $temp[$x];
         $int[] = $x;
   }
 }
}

I don't know how I should manipulate it, or how I will store it properly, I have try some array function but it gives different result to my data

Upvotes: 1

Views: 481

Answers (3)

Dilraj Singh
Dilraj Singh

Reputation: 1011

This code will work to collect duplicate in the array on the basis of id and date

$newTemp = array();
foreach($temp as $value){
  $newTemp[$value['id'].'_'.$value['date']][] = $value;  
}

Upvotes: 1

axiac
axiac

Reputation: 72226

I just want to collect data that has the same bio_id and date

The easiest way is to iterate over the input array and aggregate the data into a new array, indexed by key generated using the bio_id and date fields. This way, a duplicate entry can be easily identified because the key already exists in the output array.

$input = array(/* the big input array here */);
// Build the output here
$output = array();    
foreach ($input as $item) {
    $key = $item['bio_id'].':'.$item['date'];

    if (array_key_exists($key, $output)) {
        // This is a duplicate
        // Ignore the data, update only the count
        $output[$key]['count'] ++;
    } else {
        // This is the first time this combination is processed
        // Copy the input
        $output[$key] = $item;
        // Keep in 'count' how many times this combination exists in the input
        $output[$key]['count'] = 1;
    }
}

Each entry of $output is the first entry of $input that has the same combination of bio_id and date. Additional, the value of count is the number of entries of $input that share that pair of bio_id and date.

Work on this example if you need to aggregate the data in a different way (keep all duplicates, instead of their number, f.e.).

Another example that keeps the duplicates:

// Build the output here
$output = array();    
foreach ($input as $item) {
    $key = $item['bio_id'].':'.$item['date'];

    if (array_key_exists($key, $output)) {
        // This is a duplicate; add it to the list
        $output[$key]['all'][] = $item;
    } else {
        // This is the first time this combination is processed
        // Copy important values (bio_id and date) from the input
        $output[$key] = array(
            'bio_id' => $item['bio_id'],
            'date'   => $item['date'],
            // Store the entire $item into a list
            'all'    => array($item),
        );
    }
}

Read about PHP arrays how to access their elements using the square brackets syntax and how to create or modify their values.

Upvotes: 0

Tarun
Tarun

Reputation: 3165

$newTemp = array();
for($temp as $value){
  $key = $value->id." ".$value->date;
  if(isset($newTemp[$key])){
    $newTemp[$key] = array_merge($newTemp[$key],$value);
  }else{
    $newTemp[$key] = $value;
  }

}

Upvotes: 0

Related Questions