首页 文章

PHP比较后续的数组元素

提问于
浏览
0

所以我无法获得一些代码来工作 . 基本上我想做的是:

在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

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

    我很惊讶,如果没有这个,我没有抛出任何错误....我认为这可能会让我失望,因为它决定了它原来不起作用的原因 .

相关问题