根据数组PHP中的键添加条目

时间:2015-09-07 08:30:32

标签: php arrays

正如你所看到的,我有一个名为dateTime的键,它保存日期。我需要做的是为每个日期添加所有条目并返回数组。所以我每个日期只有一个日期时间,当天的所有条目都加在一起。我不知道dateTime输入提前,因为我有一个不断将数据插入我的数据库的爬虫。

您将如何以最有效的方式实现这一目标?我想我必须做一个foreach循环,并以某种方式检查键值(dateTime)是否从前一个键值改变。然后创建一个我返回的全新数组

数组的一个例子如下:

array (size=130)


 0 => 
    array (size=2)
      'dateTime' => string '2015-09-01' (length=10)
      'entries' => string '225' (length=3)
  1 => 
    array (size=2)
      'dateTime' => string '2015-09-01' (length=10)
      'entries' => string '218' (length=3)
  2 => 
    array (size=2)
      'dateTime' => string '2015-09-01' (length=10)
      'entries' => string '217' (length=3)
  3 => 
    array (size=2)
      'dateTime' => string '2015-09-01' (length=10)
      'entries' => string '225' (length=3)
  4 => 
    array (size=2)
      'dateTime' => string '2015-09-02' (length=10)
      'entries' => string '231' (length=3)
  5 => 
    array (size=2)
      'dateTime' => string '2015-09-02' (length=10)
      'entries' => string '220' (length=3)
  6 => 
    array (size=2)
      'dateTime' => string '2015-09-03' (length=10)
      'entries' => string '223' (length=3)
  7 => 
    array (size=2)
      'dateTime' => string '2015-09-03' (length=10)
      'entries' => string '237' (length=3)
  8 => 
    array (size=2)
      'dateTime' => string '2015-09-03' (length=10)
      'entries' => string '220' (length=3)

所以它返回一个像这样的数组

array (size=130)

  0 => 
    array (size=2)
      'dateTime' => string '2015-09-01' (length=10)
      'entries' => string '660' (length=3)
  1 => 
    array (size=2)
      'dateTime' => string '2015-09-02' (length=10)
      'entries' => string '451' (length=3)
  2 => 
    array (size=2)
      'dateTime' => string '2015-09-03' (length=10)
      'entries' => string '680' (length=3)

编辑在你们的帮助下,我最终做了以下工作,以获得正确的格式。

function prepareArrayForGraphDates($array){
$results = [];
$finalResult = [];

foreach ($array as $value) {
    if (!isset($results[$value['dateTime']])) {
        $results[$value['dateTime']] = 0;
    }
    $results[$value['dateTime']] += $value['entries'];    
}
$keyNames = array_keys($results);
for ($x = 0; $x < sizeof($results); $x++) {
    $finalResult[$x]['dateTime'] = $keyNames[$x];
   $finalResult[$x]['entries'] = $results[$keyNames[$x]];
}
return $finalResult;
}

2 个答案:

答案 0 :(得分:1)

使用数组键是唯一的事实......

$results = [];
foreach ($array as $value) {
    if (!isset($results[$value['dateTime']])) {
        $results[$value['dateTime']] = 0;
    }
    $results[$value['dateTime']] += $value['entries'];
}

答案 1 :(得分:0)

您可以使用日期作为键来使用类似集合或字典的数组。

$data = [
  [
    'datetime'=>'2015-09-02',
    'entries' => 220
  ]
  // ... other entries snipped ...
];

$results = [];
foreach ($data as $date => $entry) 
{
      // if the date has already been recorded with an initial value, add to it
      if (array_key_exists($date, $results) {
          $results[$date] += $entry
      } else {
          // the date is new; add it to the results and set its initial value
          $results[$date] = $entry;
      }

}