从 PHP 中的下一个 foreach 项目中提取元素?
我正在为我的网站做一个 PHP“红绿灯”风格的警告系统,基本上是说“如果当前数组条目和下一个数组条目之间存在 X 百分比变化,则抛出错误”。
因此,我在 foreach 循环中循环遍历数组元素,但是需要能够执行以下操作:(注意:这只是一个基本示例,但应该足以理解这个想法)
foreach($array as $a)
{
$thisValue = $a['value'];
$nextValue = next($a['value']);
$percentageDiff = ($nextValue-$thisValue)/$thisValue;
}
我接下来() 标签来获取下一个值,但了解这只适用于数组。我还可以用其他东西来获取下一个 foreach 项目吗?
感谢您抽出时间!
I'm doing a PHP 'traffic light' style warning system for my website, that basically says' if there is X percentage change between the current array entry and the next one, throw an error'.
So, I'm looping through my array elements in a foreach loop, however need to be able to do something like this: (note: this is just a basic example, but should be enough to get the idea)
foreach($array as $a)
{
$thisValue = $a['value'];
$nextValue = next($a['value']);
$percentageDiff = ($nextValue-$thisValue)/$thisValue;
}
I've put next() tags to get the next value but understand this only works for arrays. IS there something else I can use to get the next foreach item?
Thanks for your time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
以另一种方式进行,存储之前的条目并进行比较。
do it the other way, and store the previous entry and compare those.
简单的答案:不要使用
foreach
循环。使用简单的for
循环代替:Simple answer: don't use a
foreach
loop. Use a simplefor
loop instead:您应该能够使用:
这会复制数组,然后将指针移动 1。
You should be able to use:
This copies the array and then moves the pointer along by 1.
在我看来,最简单的解决方案就是改变你的心态。检查上一条记录和当前记录,而不是检查当前记录和下一条记录。记住上一个比记住下一个更容易。
如果您不希望这样,您也可以放弃
foreach
并使用for
和计数器变量迭代 C 风格 - 但需要注意的是:PHP 的稀疏数组可能会困扰您,因此您最好在迭代之前对检查的数组调用array_values()
。The easiest solution IMO is to change your mindset. Instead of inspecting the current and the next record, inspect the previous and the current record. Remembering the previous one is easier than getting the next one.
If you don't want that, you can also ditch the
foreach
and iterate C-style usingfor
and a counter variable - one cavet though: PHP's sparse arrays can bite you, so you best callarray_values()
on the inspected array before iterating.如果您想使用
foreach
,您可以将当前值与前一个值进行比较,而不是与下一个值进行比较:这样您只需将整个迭代移动一项。
If you want to work with
foreach
, you could compare the current value with the previous value instead of with the next value:With this you just shift the whole iteration by one item.
为什么不直接使用普通的 for 循环而不是 foreach 迭代器呢?
Why not just use a normal for loop rather than the foreach iterator?
'通常使用前一个值比使用下一个值更容易:
'It is usually easier to use the previous value than the next:
确实有 数组迭代器 函数可以支持您所需要的,而且也很简单使用 next() / prev() 等循环遍历数组工作正常,但上面的解决方案更优雅
它不适用于 foreach,因为 foreach 创建引用的副本并且不会在数组本身中设置数组指针。
以下是可用于使用数组迭代器函数的关联数组的示例:
There are indeed array iterator functions which support what you need, and also simply looping thorugh an array with next() / prev() etc works fine but the solution above is more elegant
It doesn't work with foreach because foreach creates a copy of references and doesn't set the array pointers in the array itself.
Here is a sample which can be used for associative arrays using array iterator functions: