Php 如何计算数组中的两个连续值
我试图循环遍历一个数组,并用下一个数组计算每个值。数据如下Php 如何计算数组中的两个连续值,php,arrays,laravel,Php,Arrays,Laravel,我试图循环遍历一个数组,并用下一个数组计算每个值。数据如下 [ ['rate' => 1000, 'date' => '2017-07-10'], ['rate' => 2000, 'date' => '2017-08-14'], ['rate' => 3000, 'date' => '2017-08-18'], ['rate' => 1000, 'date' => '2017-07-23'] ] 我已经编辑了这个[根据sta
[
['rate' => 1000, 'date' => '2017-07-10'],
['rate' => 2000, 'date' => '2017-08-14'],
['rate' => 3000, 'date' => '2017-08-18'],
['rate' => 1000, 'date' => '2017-07-23']
]
我已经编辑了这个[根据stackoverflow中另一个问题的例子]:
@foreach ($users as $user)
@if ($user->admin_id == $active_user) // filtering users under this admin
@foreach ($userbillings as $userbilling)
@if($userbilling->user_id == $user->id) // filtering users
<?php
$count = count($userbilling->created_at);
$rates[] = $userbilling->rate;
$diff = 0;
for ($i=0; $i < $count; $i++) {
if (isset($rates[$i + 1])) {
$thisValue = $rates[$i];
$nextValue = $rates[$i + 1];
$diff = $nextValue - $thisValue;
}
}
echo $diff;
?>
@endif
@endforeach
@endif
@endforeach
@foreach($users作为$user)
@if($user->admin\u id==$active\u user)//筛选此管理员下的用户
@foreach($userbillings作为$userbilling)
@if($userbilling->user\u id==$user->id)//筛选用户
@恩迪夫
@endforeach
@恩迪夫
@endforeach
这给了我一个结果:1000 2000 3000
我想要实现的是从第二项中减去第一项,从第三项中减去第二项,从第四项中减去第三项,依此类推,直到最后一项。最后一项,我想保留整数
我知道我需要在这里使用数组和循环,在过去的两周里我尝试了不同的方法,但都无法接近
请建议我如何使它工作,或给我一个指导方针,我应该如何进行。我在这里搜索了很多问题,但找不到任何适合我的问题。如果有,请给我推荐链接。您的问题不清楚,但这里有一个可能对您有所帮助的示例 使用
array\u slice()
和array\u sum()
$array=array(“0”=>1,“1”=>1,“2”=>5,“3”=>1,“4”=>1,“7”=>1,“8”=>3,“9”=>1);
$keys=数组\ U键($array);
$array=数组值($array);
$newArr=array();
foreach($key=>$val的数组){
$newArr[]=array_sum(array_slice($array,0,$key+1));
}
$newArr=array\u combine($key,$newArr);
打印“”;
印刷费($newArr);
打印“”;
参考:
<?php
$periods = [
['rate' => 1000, 'date' => '2017-07-14'],
['rate' => 3000, 'date' => '2017-08-18'],
['rate' => 2000, 'date' => '2017-08-10'],
['rate' => 3000, 'date' => '2017-08-15'],
['rate' => 1000, 'date' => '2017-08-23'],
];
$lastPeriod = count($periods) - 1;
$ratesForPeriods = [];
foreach ($periods as $index => $currentPeriod) {
if ($index === $lastPeriod) {
$ratesForPeriods[] = $currentPeriod; // add the last as-is
} else {
$nextPeriod = $periods[$index + 1];
$currentDate = new DateTime($currentPeriod['date']);
$interval = $currentDate->diff(new DateTime($nextPeriod['date']));
$ratesForPeriods[] = [
'from' => $currentPeriod['date'],
'to' => $nextPeriod['date'],
'rate' => $currentPeriod['rate'],
'days' => $interval->days,
'total' => $interval->days * $currentPeriod['rate'],
];
}
}
print_r($ratesForPeriods);
您尚未指定首选输出,但假设您希望最终得到所有对的计算的数组结果,加上最后一个元素,这将使您走上正确的轨道 我还要附和上面的一条评论——不要在视图中执行这种逻辑,因为它属于控制器或服务之类的地方。让您的视图尽可能简单 未经测试,请自行测试:
Array
(
[0] => Array
(
[from] => 2017-07-14
[to] => 2017-08-18
[rate] => 1000
[days] => 35
[total] => 35000
)
[1] => Array
(
[from] => 2017-08-18
[to] => 2017-08-10
[rate] => 3000
[days] => 8
[total] => 24000
)
[2] => Array
(
[from] => 2017-08-10
[to] => 2017-08-15
[rate] => 2000
[days] => 5
[total] => 10000
)
[3] => Array
(
[from] => 2017-08-15
[to] => 2017-08-23
[rate] => 3000
[days] => 8
[total] => 24000
)
[4] => Array
(
[rate] => 1000
[date] => 2017-08-23
)
)
希望这有帮助:)你能提供一个数据示例吗?基本上这就是我想要计算的。第一列是费率,第二列是日期。我想找出第一次约会和第二次约会、第二次约会和第三次约会之间的区别,依此类推。并根据差值1000 2017-07-14 3000 2017-08-18 2000 2017-08-10 3000 2017-08-15 1000 2017-08-23列计算费率?用一个数据结构的例子更新你的问题,我们可能会帮助你-否则每个人都会猜。你在你的视图中做了太多的逻辑,这需要在你的控制器中,你应该为用户使用关系,并有一个用户->billings关系,这对我有很大帮助。非常感谢你。这里还有很多计算要做[我在管理员保护下过滤用户,然后过滤每个用户的速率]。完成后,我将发表评论并接受您的回答。谢谢。但它不起作用,因为$key是此处行的id。但行不是顺序的,因为我正在为每个用户筛选结果。因此,我没有使用[$key+1],而是在寻找另一种方法来找到下一个$key。这将解决我的问题请尝试
current()
和next()
函数。谢谢。虽然它在这个特殊的问题上不起作用,但我想我将来会需要它。非常感谢。它向我展示了解决问题的正确方法,即使我的问题不清楚。此外,我会按照你的建议,把逻辑控制器。
<?php
$periods = [
['rate' => 1000, 'date' => '2017-07-14'],
['rate' => 3000, 'date' => '2017-08-18'],
['rate' => 2000, 'date' => '2017-08-10'],
['rate' => 3000, 'date' => '2017-08-15'],
['rate' => 1000, 'date' => '2017-08-23'],
];
$lastPeriod = count($periods) - 1;
$ratesForPeriods = [];
foreach ($periods as $index => $currentPeriod) {
if ($index === $lastPeriod) {
$ratesForPeriods[] = $currentPeriod; // add the last as-is
} else {
$nextPeriod = $periods[$index + 1];
$currentDate = new DateTime($currentPeriod['date']);
$interval = $currentDate->diff(new DateTime($nextPeriod['date']));
$ratesForPeriods[] = [
'from' => $currentPeriod['date'],
'to' => $nextPeriod['date'],
'rate' => $currentPeriod['rate'],
'days' => $interval->days,
'total' => $interval->days * $currentPeriod['rate'],
];
}
}
print_r($ratesForPeriods);
Array
(
[0] => Array
(
[from] => 2017-07-14
[to] => 2017-08-18
[rate] => 1000
[days] => 35
[total] => 35000
)
[1] => Array
(
[from] => 2017-08-18
[to] => 2017-08-10
[rate] => 3000
[days] => 8
[total] => 24000
)
[2] => Array
(
[from] => 2017-08-10
[to] => 2017-08-15
[rate] => 2000
[days] => 5
[total] => 10000
)
[3] => Array
(
[from] => 2017-08-15
[to] => 2017-08-23
[rate] => 3000
[days] => 8
[total] => 24000
)
[4] => Array
(
[rate] => 1000
[date] => 2017-08-23
)
)