1

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

12
  • try using array_unique() function ... Commented Oct 18, 2017 at 6:00
  • I cannot spot any duplicate in the data you posted. Maybe you should define what do you mean by "duplicate" and how looks the expected output. Commented Oct 18, 2017 at 6:00
  • @RamuBhusal array_unique() doesn't help. There are no duplicates in the posted array. Commented Oct 18, 2017 at 6:02
  • I just updated my question, sorry for the confusing question Commented Oct 18, 2017 at 6:03
  • "I just want to collect data that has the same bio_id and date" -- what about the other fields? They are different on entries that have the same bio_id and date. How do you want to store them? Commented Oct 18, 2017 at 6:05

3 Answers 3

1

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;  
}
Sign up to request clarification or add additional context in comments.

12 Comments

I want to collect the duplicate data, not on removing them
I have updated the code to collect the duplicate data.
for is not foreach
it return only one data
can I request this in for loop, I just want to manipulate it, and i'm not good in using foreach?
|
0
$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;
  }

}

Comments

0

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.

5 Comments

Can I request this in for loop?
foreach is the natural PHP way to process arrays. for cannot be used if the array keys are not consecutive numbers starting with 0.
it has an error "unexpected }", it was in the if part, I don't know why it has an error like that
$input = $temp; $output = array(); foreach ($input as $item) { $key = $item['bio_id'].':'.$item['date']; if (array_key_exists($key, $output) { $output[$key]['count'] ++; } else { $output[$key] = $item; $output[$key]['count'] = 1; } }
A closing parenthesis was missing in the if line. I fixed it now and I added another example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.