Php 从mysql结果填充Arrray
从mysql查询中,我得到了这个数组Php 从mysql结果填充Arrray,php,mysql,arrays,Php,Mysql,Arrays,从mysql查询中,我得到了这个数组 ( [0] => Array ( [time] => 00:00:00 [sales] => 55.99 [orders] => 1 ) [1] => Array ( [time] => 06:00:00 [sales] => 46.37 [orde
(
[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小时。我更喜欢教人钓鱼,但因为这是相当具体的,不管怎样 请记住,这个答案不利于复制/粘贴,因为我没有为您完成所有工作。您仍然需要执行大量的检查,以防止通知在与预期数据稍有偏差的情况下左右抛出 另外,下面是一个3v4l代码 我假设您的数据:
$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中的“奇数”行为,将
字符串
强制转换为int
将“切掉”无法解析的字符。另一个例子是(int)“123abc”
,它将产生int(123)
。因此,在我的例子中,(int)'12:00:00'
只会产生int(12)
,它适用于本例中的排序。这可能不是最好的解决方案,但考虑到我的理解:
//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);
}
}
已编辑
分解逻辑,这样就不会重复内部循环中的赋值。这是可能的。有什么想法吗?好的,如果可能的话,我可以使用哪种数组函数?在数组中循环,并检查是否与数组中的下一项有2小时的差异。如果没有,请创建该项。@Ronny Linsener:你真的希望看到
fillMyArrayWith2HoursGapsPlease
内置函数吗?我不知道为什么,但现在数组循环了44次。我认为这是原始数组的4个索引的11倍,由您的示例所激发。我用一个非常短的代码foreach($times as$time){if($sales['RESULT'][$time]['time]['time\u purchased']=$time){$new[$time]=$sales['RESULT'][$time];}否则{$new time[$time]=array('time_purchased'=>$time',sales_today'=>'0','orders_today'=>'0',)}$orders_today阵列[]=$sales['RESULT'][$time]['orders_today']?$sales['RESULT'][$time]['orders_today']:0;$sales['RESULT'][$time][$time][$sales[$time][$sales[$time]['today']:0;]
如果你想说明订单的原因,你只需将其安排到位,还是选择其他方式?喜欢这个吻,一步一个脚印……我仍然倾向于过度复杂。@FernandoSilva,这是正确的;我把它作为操作练习;-)