2017-10-18 11 views
1

这是从生物指标,我的样品阵列数据 我只是想收集具有相同bio_id和日期使用PHP

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" 
} 
] 

我的数据收集在阵列重复数据一直坚持使用我编写的代码,不知道该如何操作它,或者我将如何正确存储它,我尝试了一些数组函数,但它给我的数据提供了不同的结果

$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; 
    } 
} 
} 

我不知道我应该怎么操作呢,还是我将如何妥善存放,我有尝试一些阵列功能,但它提供了不同的结果,我的数据

+0

尝试使用array_unique()函数... –

+0

我不能当场数据中的任何重复你贴。也许你应该定义“重复”是什么意思,以及如何看待预期的输出。 – axiac

+0

@RamuBhusal ['array_unique()'](http://php.net/manual/en/function.array-unique.php)没有帮助。在发布的数组中没有重复项。 – axiac

回答

1

此代码将努力收集阵列中的副本ID和日期的基础上

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

我想收集重复数据,而不是删除它们 – MariaJen

+0

我已更新代码以收集重复数据。 – user2613580

+0

['for'](http://php.net/manual/en/control-structures.for.php)不是['foreach'](http://php.net/ manual/en/control-structures.foreach.php) – axiac

0

我只是想收集具有相同bio_iddate

最简单的方法是将数据遍历输入数组,并聚集成一个新的数据数组,使用bio_iddate字段生成的密钥索引。这样,重复的条目可以很容易地识别,因为该键已经存在于输出数组中。

$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; 
    } 
} 

$output每个条目是具有的bio_iddate相同组合的$input的第一个条目。此外,count的值是共享该对bio_iddate的条目$input的条目数。

如果您需要以不同方式聚合数据(保留所有重复项,而不是其编号,f.e.),请参阅本示例。

,保持了重复又如:

// 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), 
     ); 
    } 
} 

阅读关于PHP arrays如何访问使用square brackets syntax,并create or modify their values他们的元素如何。

+0

我可以在循环中请求这个吗? – MariaJen

+0

我不擅长使用foreach – MariaJen

+0

[''foreach'](http://php.net/manual/en/control-structures.foreach.php)是处理数组的自然PHP方法。如果数组键不是以'0'开始的连续数字,则不能使用'for'。 – axiac

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; 
    } 

}