从 PHP 中的下一个 foreach 项目中提取元素?

发布于 2024-10-13 05:29:26 字数 398 浏览 3 评论 0原文

我正在为我的网站做一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

余罪 2024-10-20 05:29:26

以另一种方式进行,存储之前的条目并进行比较。

$prev = null;
foreach ($item as $i){
    if ($prev !== null){
        diff($prev, $i);
    }
    $prev = $i
}

do it the other way, and store the previous entry and compare those.

$prev = null;
foreach ($item as $i){
    if ($prev !== null){
        diff($prev, $i);
    }
    $prev = $i
}
痴梦一场 2024-10-20 05:29:26

简单的答案:不要使用 foreach 循环。使用简单的 for 循环代替:

for ($i = 0; $i < count($array); $i++) {
    if (isset($array[$i + 1])) {
        $thisValue = $array[$i]['value'];
        $nextValue = $array[$i + 1]['value'];

        $percentageDiff = ($nextValue-$thisValue)/$thisValue;
    }
}

Simple answer: don't use a foreach loop. Use a simple for loop instead:

for ($i = 0; $i < count($array); $i++) {
    if (isset($array[$i + 1])) {
        $thisValue = $array[$i]['value'];
        $nextValue = $array[$i + 1]['value'];

        $percentageDiff = ($nextValue-$thisValue)/$thisValue;
    }
}
妄司 2024-10-20 05:29:26

您应该能够使用:

foreach($array as $a)
{
    $array_copy = $array;
    $thisValue = $a['value'];

    $nextValue = next($array_copy);
    $nextValue = $nextValue['value'];

    $percentageDiff = ($nextValue-$thisValue)/$thisValue;
}

这会复制数组,然后将指针移动 1。

You should be able to use:

foreach($array as $a)
{
    $array_copy = $array;
    $thisValue = $a['value'];

    $nextValue = next($array_copy);
    $nextValue = $nextValue['value'];

    $percentageDiff = ($nextValue-$thisValue)/$thisValue;
}

This copies the array and then moves the pointer along by 1.

凉风有信 2024-10-20 05:29:26

在我看来,最简单的解决方案就是改变你的心态。检查上一条记录和当前记录,而不是检查当前记录和下一条记录。记住上一个比记住下一个更容易。

如果您不希望这样,您也可以放弃 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 using for and a counter variable - one cavet though: PHP's sparse arrays can bite you, so you best call array_values() on the inspected array before iterating.

小…楫夜泊 2024-10-20 05:29:26

如果您想使用 foreach,您可以将当前值与前一个值进行比较,而不是与下一个值进行比较:

$previous = null;
foreach ($array as $a) {
    if (!is_null($previous)) {
        $thisValue = $previous['value'];
        $nextValue = $a['value'];
        $percentageDiff = ($nextValue-$thisValue)/$thisValue;
    }
    $previous = $a;
}

这样您只需将整个迭代移动一项。

If you want to work with foreach, you could compare the current value with the previous value instead of with the next value:

$previous = null;
foreach ($array as $a) {
    if (!is_null($previous)) {
        $thisValue = $previous['value'];
        $nextValue = $a['value'];
        $percentageDiff = ($nextValue-$thisValue)/$thisValue;
    }
    $previous = $a;
}

With this you just shift the whole iteration by one item.

梦醒时光 2024-10-20 05:29:26

为什么不直接使用普通的 for 循环而不是 foreach 迭代器呢?

<?php
    $testArray = array(10,20,30,40,50,60,70,80,90,100);

    $elementCount = count($testArray);
    for($loop=0; $loop<$elementCount; $loop++) {

        $thisValue = $testArray[$loop];

        // Check if there *is* a next element.
        $nextValue = $loop + 1 < $elementCount ? $testArray[$loop + 1] : null;

        // Calculate the percentage difference if we have a next value.
        if($nextValue) $percentageDiff = ($nextValue-$thisValue)/$thisValue;
    }
?>

Why not just use a normal for loop rather than the foreach iterator?

<?php
    $testArray = array(10,20,30,40,50,60,70,80,90,100);

    $elementCount = count($testArray);
    for($loop=0; $loop<$elementCount; $loop++) {

        $thisValue = $testArray[$loop];

        // Check if there *is* a next element.
        $nextValue = $loop + 1 < $elementCount ? $testArray[$loop + 1] : null;

        // Calculate the percentage difference if we have a next value.
        if($nextValue) $percentageDiff = ($nextValue-$thisValue)/$thisValue;
    }
?>
只想待在家 2024-10-20 05:29:26

'通常使用前一个值比使用下一个值更容易:

$lastValue = NULL;
foreach ($array as $a) {
    if ($lastValue === NULL) {
        $lastValue = $a['value'];
        continue;
    }

    $percentageDiff = ($a['value'] - $lastValue) / $lastValue;

    $lastValue = $a['value'];
}

'It is usually easier to use the previous value than the next:

$lastValue = NULL;
foreach ($array as $a) {
    if ($lastValue === NULL) {
        $lastValue = $a['value'];
        continue;
    }

    $percentageDiff = ($a['value'] - $lastValue) / $lastValue;

    $lastValue = $a['value'];
}
简单 2024-10-20 05:29:26
for($i=0;$i<count($array);$i++) {
$thisValue = $array[$i];
$nextValue = $array[i+1]; // will not be set if $i==count($array)-1
$percentageDiff = ($nextValue-$thisValue)/$thisValue;
}

确实有 数组迭代器 函数可以支持您所需要的,而且也很简单使用 next() / prev() 等循环遍历数组工作正常,但上面的解决方案更优雅

它不适用于 foreach,因为 foreach 创建引用的副本并且不会在数组本身中设置数组指针。

以下是可用于使用数组迭代器函数的关联数组的示例:

$arrayobject = new ArrayObject($array);
$iterator = $arrayobject->getIterator();

        for($i=0;$i<count($array);$i++) {
           $iterator->seek($i);
           $thisValue = $iterator->current();
           $iterator->seek($i+1);
           $nextValue = $iterator->current(); 
           $percentageDiff = ($nextValue-$thisValue)/$thisValue;
        }
for($i=0;$i<count($array);$i++) {
$thisValue = $array[$i];
$nextValue = $array[i+1]; // will not be set if $i==count($array)-1
$percentageDiff = ($nextValue-$thisValue)/$thisValue;
}

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:

$arrayobject = new ArrayObject($array);
$iterator = $arrayobject->getIterator();

        for($i=0;$i<count($array);$i++) {
           $iterator->seek($i);
           $thisValue = $iterator->current();
           $iterator->seek($i+1);
           $nextValue = $iterator->current(); 
           $percentageDiff = ($nextValue-$thisValue)/$thisValue;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文