PHP比较后续的数组元素

时间:2014-11-26 20:16:21

标签: php arrays ebay-api

因此我无法获得一些代码。基本上我想做的是:

在foreach循环中,如果设置了给定的数组值,则将该现有值与当前循环值进行比较,如果现有值已经大于当前值,则设置现有值=当前值(对于迭代)。以下是我正在使用的代码:

if ($usedebayxml->ack == 'Success') {
    foreach($usedebayxml->searchResult->item as $key => $value) {
        if(isset($newarray[1]['TotalCost'])) {
            if($newarray[1]['TotalCost'] > ((integer)$value->shippingInfo->shippingServiceCost + (integer)$value->sellingStatus->currentPrice)) {
                $newarray[1]['Title'] = (string)$value->title ;
                $newarray[1]['ShippingCost'] = (integer)$value->shippingInfo->shippingServiceCost;
                $newarray[1]['Price'] = (integer)$value->sellingStatus->currentPrice;
                $newarray[1]['Condition'] = 'New';
                $newarray[1]['TotalCost'] = (integer)$value->shippingInfo->shippingServiceCost + (integer)$value->sellingStatus->currentPrice;
            }
        }
        else
            $newarray[1]['Title'] = (string)$value->title;
            $newarray[1]['ShippingCost'] = (integer)$value->shippingInfo->shippingServiceCost;
            $newarray[1]['Price'] = (integer)$value->sellingStatus->currentPrice;
            $newarray[1]['Condition'] = 'Used';
            $newarray[1]['TotalCost'] = (integer)$value->shippingInfo->shippingServiceCost + (integer)$value->sellingStatus->currentPrice;
    }
}

使用此代码,返回的内容最终是xml文件中LAST密钥对象中的值(如果有帮助,则使用simpleXML)。换句话说,我不认为正在输入第一个if块(如果是isset),并且值被设置为当前迭代的值。任何人都可以在我的逻辑中看到任何缺陷吗?我已经被困在这一段了一段时间。

1 个答案:

答案 0 :(得分:0)

我是一个至高无上的白痴。这里的逻辑很好,我只是错过了{对于开放的其他区块。杜尔!添加后,这段代码按预期工作:)

我很惊讶,虽然没有这个但我没有犯错误......我认为这可能会让我失望,因为它决定了它原来不起作用的原因。