从mysql结果中填充一个数组

时间:2014-02-06 22:14:07

标签: php mysql arrays

从我的mysql查询中得到这个数组

(
  [0] => Array
      (
          [time] =>  00:00:00
          [sales] => 55.99
          [orders] => 1
      )

  [1] => Array
      (
          [time] =>  06:00:00
          [sales] => 46.37
          [orders] => 1
      )

  [2] => Array
      (
          [time] =>  08:00:00
          [sales] => 246.56
          [orders] => 4
      )

  [3] => Array
      (
          [time] =>  10:00:00
          [sales] => 78.66
          [orders] => 1
      )

)

是否可以用缺少的时间值填充数组(例如02:00:00,04:00:00) 所以我会有一个2小时的完整数组。

2 个答案:

答案 0 :(得分:1)

我更喜欢教一个人钓鱼,但因为这是相当具体的,无论如何。

请注意,这个答案复制/粘贴友好,因为我没有为您完成所有工作。你需要实施大量的检查,以防止通知被左右抛出,与预期的数据有最小的偏差。

此外,这是以下代码的3v4l; http://3v4l.org/RrNKA

您的数据,我认为:

$rows = [
    [
        'time'   => '00:00:00',
        'sales'  => 55.99,
        'orders' => 1,
    ],
    [
        'time'   => '06:00:00',
        'sales'  => 46.37,
        'orders' => 1,
    ],
    [
        'time'   => '08:00:00',
        'sales'  => 246.56,
        'orders' => 4,
    ],
    [
        'time'   => '10:00:00',
        'sales'  => 78.66,
        'orders' => 1,
    ]
];

然后你想要:

// isolate the times
$times = array_map(function ($row) {
    return $row['time'];
}, $rows);

// create a list of similarly formatted times from 00:00:00 - 22:00:00
$allTimes = array_map(function ($hour) {
    return sprintf('%02d:00:00', $hour);
}, range(0, 22, 2));

// compute the difference
$notTimes = array_diff($allTimes, $times);

// add the notTimes to the original rowset with default values
foreach ($notTimes as $notTime)
{
    $rows[] = [
        'time'   => $notTime,
        'sales'  => 0,
        'orders' => 0,
    ];
}

// sort, because why not
usort($rows, function ($rowOne, $rowTwo) {
    return ((int) $rowOne['time']) > ((int) $rowTwo['time']); // refer to note
});

// dump
var_dump($rows);

其中,转储时产生的结果如下:

.
.
.
[2] =>
array(3) {
  'time' =>
  string(8) "08:00:00"
  'sales' =>
  double(246.56)
  'orders' =>
  int(4)
}
[3] =>
array(3) {
  'time' =>
  string(8) "10:00:00"
  'sales' =>
  double(78.66)
  'orders' =>
  int(1)
}
[4] =>
array(3) {
  'time' =>
  string(8) "02:00:00"
  'sales' =>
  int(0)
  'orders' =>
  int(0)
}
.
.
.

注意 - 不要这样排序。这种方法虽然有效,却利用PHP中的“奇怪”行为,将string转换为int将“切断”无法解析的字符。另一个例子是(int) "123abc",它会产生int(123)。因此,在我的情况下,(int) '12:00:00'只会产生int(12),这适用于此示例中的排序。

答案 1 :(得分:0)

这可能不是最佳解决方案,但鉴于我在此理解的内容:

//your result array
$resultArray = array();
//desired times
$times = array( 
    '00:00:00', 
    '02:00:00', 
    '04:00:00', 
    '06:00:00', 
    '10:00:00', 
    '12:00:00', 
    '14:00:00', 
    '16:00:00', 
    '18:00:00', 
    '20:00:00', 
    '22:00:00' 
);
//new array with desired order and values
$newResult = array();
//loop through times
foreach ($times as $v) {
    $match;
    $innerKey;
    //then run through results to check them against the desired times
    foreach ($resultArray as $key=>$value) {
        //verify if the value aleady exists within the resultArray
        $innerKey = $key;
        if ($resultArray[$key]['time_purchased'] == $v) {
            $match = true;
            //stop the foreach if you find a match
            return;
        } else {
            $match = false;
        }
    }
    //either use the result value or an empty array with the time
    if ($match){
        array_push($newResult, $resultArray[$innerKey]);
    } else {
        $empty = array( 
            'time'=>$v,
            'sales'=>0,
            'orders'=>0
        );
        array_push($newResult, $empty);
    }
}

<强>被修改 打破逻辑,这样它就不会重复内循环中的赋值。