比较当前数组元素与先前数组元素

时间:2019-05-16 20:35:47

标签: php

我正在经历一个foreach循环,如下所示:

$ticks = $api->candle("APPLUSD", "4h", "10");
foreach($ticks as $tick)
{
    //do something here
}

在每个刻度内,我都有蜡烛的开盘价和收盘价。我想尝试确定我的foreach循环中打开和关闭值中的当前数组值是否大于我刚刚处理的最后一个值中的上一个打开和关闭值。

2 个答案:

答案 0 :(得分:1)

您需要通过初始化$last_close$ticks = $api->candle("APPLUSD", "4h", "10"); $last_open = $last_close = // insert largest value foreach($ticks as $tick) { if (($tick->open > $last_open) && ($tick->close > $last_close)) { // do something } $last_open = $tick->open; $last_close = $tick->close; }

处理第一次迭代
uart6: serial@48068000 {
        compatible = "ti,dra742-uart", "ti,omap4-uart";
        reg = <0x48068000 0x100>;
        interrupts = <GIC_SPI 101 IRQ_TYPE_LEVEL_HIGH>;
        ti,hwmods = "uart6";
        clock-frequency = <48000000>;
        status = "disabled";
        dmas = <&sdma_xbar 79>, <&sdma_xbar 80>;
        dma-names = "tx", "rx";
    };

答案 1 :(得分:1)

也许使用临时变量,例如“ last_tick”?

$ticks = $api->candle("APPLUSD", "4h", "10");
$last_tick = null;
foreach($ticks as $tick)
{
    if (!is_null($last_tick)) {
      //do something here
    }
    $last_tick = $tick;
}