数组项值的总和

时间:2019-01-26 14:44:07

标签: php arrays

我有一系列来自ebay api的已售产品。所以我想让( value )的总和searchResult->item->shippingInfo成立。我已经尝试过了,但是没有用

$value = array_sum(array_column($arr,'_value_'));

物品的实际价格。我给出了两个项目的示例,但是有100个项目。 foreach的一种方法,但是却变慢了

Array
(
[findCompletedItemsResponse] => Array
    (
        [0] => Array
            (
                [ack] => Array
                    (
                        [0] => Success
                    )

                [version] => Array
                    (
                        [0] => 1.13.0
                    )

                [timestamp] => Array
                    (
                        [0] => 2019-01-26T14:38:08.292Z
                    )

                [searchResult] => Array
                    (
                        [0] => Array
                            (
                                [@count] => 5
                                [item] => Array
                                    (
                                        [0] => Array
                                            (
                                                [itemId] => Array
                                                    (
                                                        [0] => 303031907318
                                                    )

                                                [title] => Array
                                                    (
                                                        [0] => Atocha 1622 Mel Fisher Real Pendant with C.O.A.
                                                    )

                                                [globalId] => Array
                                                    (
                                                        [0] => EBAY-US
                                                    )

                                                [primaryCategory] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [categoryId] => Array
                                                                    (
                                                                        [0] => 164330
                                                                    )

                                                                [categoryName] => Array
                                                                    (
                                                                        [0] => Precious Metal without Stones
                                                                    )

                                                            )

                                                    )

                                                [secondaryCategory] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [categoryId] => Array
                                                                    (
                                                                        [0] => 39481
                                                                    )

                                                                [categoryName] => Array
                                                                    (
                                                                        [0] => Spain
                                                                    )

                                                            )

                                                    )

                                                [galleryURL] => Array
                                                    (
                                                        [0] => http://thumbs3.ebaystatic.com/m/m8YVT7tGvnJYlzJVJA8P2LA/140.jpg
                                                    )

                                                [viewItemURL] => Array
                                                    (
                                                        [0] => http://www.ebay.com/itm/Atocha-1622-Mel-Fisher-Real-Pendant-C-O-A-/303031907318
                                                    )

                                                [paymentMethod] => Array
                                                    (
                                                        [0] => PayPal
                                                    )

                                                [autoPay] => Array
                                                    (
                                                        [0] => false
                                                    )

                                                [postalCode] => Array
                                                    (
                                                        [0] => 60004
                                                    )

                                                [location] => Array
                                                    (
                                                        [0] => Arlington Heights,IL,USA
                                                    )

                                                [country] => Array
                                                    (
                                                        [0] => US
                                                    )

                                                [shippingInfo] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [shippingServiceCost] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [@currencyId] => USD
                                                                                [__value__] => 0.0
                                                                            )

                                                                    )

                                                                [shippingType] => Array
                                                                    (
                                                                        [0] => Free
                                                                    )

                                                                [shipToLocations] => Array
                                                                    (
                                                                        [0] => Worldwide
                                                                    )

                                                                [expeditedShipping] => Array
                                                                    (
                                                                        [0] => false
                                                                    )

                                                                [oneDayShippingAvailable] => Array
                                                                    (
                                                                        [0] => false
                                                                    )

                                                                [handlingTime] => Array
                                                                    (
                                                                        [0] => 1
                                                                    )

                                                            )

                                                    )

                                                [sellingStatus] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [currentPrice] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [@currencyId] => USD
                                                                                [__value__] => 65.0
                                                                            )

                                                                    )

                                                                [convertedCurrentPrice] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [@currencyId] => USD
                                                                                [__value__] => 65.0
                                                                            )

                                                                    )

                                                                [bidCount] => Array
                                                                    (
                                                                        [0] => 21
                                                                    )

                                                                [sellingState] => Array
                                                                    (
                                                                        [0] => EndedWithSales
                                                                    )

                                                            )

                                                    )

                                                [listingInfo] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [bestOfferEnabled] => Array
                                                                    (
                                                                        [0] => false
                                                                    )

                                                                [buyItNowAvailable] => Array
                                                                    (
                                                                        [0] => false
                                                                    )

                                                                [startTime] => Array
                                                                    (
                                                                        [0] => 2019-01-17T03:24:58.000Z
                                                                    )

                                                                [endTime] => Array
                                                                    (
                                                                        [0] => 2019-01-24T03:24:58.000Z
                                                                    )

                                                                [listingType] => Array
                                                                    (
                                                                        [0] => Auction
                                                                    )

                                                                [gift] => Array
                                                                    (
                                                                        [0] => false
                                                                    )

                                                                [watchCount] => Array
                                                                    (
                                                                        [0] => 7
                                                                    )

                                                            )

                                                    )

                                                [returnsAccepted] => Array
                                                    (
                                                        [0] => true
                                                    )

                                                [galleryPlusPictureURL] => Array
                                                    (
                                                        [0] => http://galleryplus.ebayimg.com/ws/web/303031907318_1_0_1.jpg
                                                    )

                                                [condition] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [conditionId] => Array
                                                                    (
                                                                        [0] => 3000
                                                                    )

                                                                [conditionDisplayName] => Array
                                                                    (
                                                                        [0] => Pre-owned
                                                                    )

                                                            )

                                                    )

                                                [isMultiVariationListing] => Array
                                                    (
                                                        [0] => false
                                                    )

                                                [topRatedListing] => Array
                                                    (
                                                        [0] => false
                                                    )

                                            )

                                        [1] => Array
                                            (
                                                [itemId] => Array
                                                    (
                                                        [0] => 283317356027
                                                    )

                                                [title] => Array
                                                    (
                                                        [0] => ATOCHA REALE SILVER COIN SPANISH TREASURE 14K GOLD PENDANT MEL FISHER 
                                                    )

                                                [globalId] => Array
                                                    (
                                                        [0] => EBAY-US
                                                    )

                                                [primaryCategory] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [categoryId] => Array
                                                                    (
                                                                        [0] => 39481
                                                                    )

                                                                [categoryName] => Array
                                                                    (
                                                                        [0] => Spain
                                                                    )

                                                            )

                                                    )

                                                [galleryURL] => Array
                                                    (
                                                        [0] => http://thumbs4.ebaystatic.com/m/mEAuJpNBAUAjzDRqn-j0WpA/140.jpg
                                                    )

                                                [viewItemURL] => Array
                                                    (
                                                        [0] => http://www.ebay.com/itm/ATOCHA-REALE-SILVER-COIN-SPANISH-TREASURE-14K-GOLD-PENDANT-MEL-FISHER-/283317356027
                                                    )

                                                [paymentMethod] => Array
                                                    (
                                                        [0] => PayPal
                                                    )

                                                [autoPay] => Array
                                                    (
                                                        [0] => false
                                                    )

                                                [postalCode] => Array
                                                    (
                                                        [0] => 46201
                                                    )

                                                [location] => Array
                                                    (
                                                        [0] => Indianapolis,IN,USA
                                                    )

                                                [country] => Array
                                                    (
                                                        [0] => US
                                                    )

                                                [shippingInfo] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [shippingServiceCost] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [@currencyId] => USD
                                                                                [__value__] => 6.0
                                                                            )

                                                                    )

                                                                [shippingType] => Array
                                                                    (
                                                                        [0] => Flat
                                                                    )

                                                                [shipToLocations] => Array
                                                                    (
                                                                        [0] => Worldwide
                                                                    )

                                                                [expeditedShipping] => Array
                                                                    (
                                                                        [0] => false
                                                                    )

                                                                [oneDayShippingAvailable] => Array
                                                                    (
                                                                        [0] => false
                                                                    )

                                                                [handlingTime] => Array
                                                                    (
                                                                        [0] => 2
                                                                    )

                                                            )

                                                    )

                                                [sellingStatus] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [currentPrice] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [@currencyId] => USD
                                                                                [__value__] => 250.0
                                                                            )

                                                                    )

                                                                [convertedCurrentPrice] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [@currencyId] => USD
                                                                                [__value__] => 250.0
                                                                            )

                                                                    )

                                                                [sellingState] => Array
                                                                    (
                                                                        [0] => EndedWithSales
                                                                    )

                                                            )

                                                    )

                                                [listingInfo] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [bestOfferEnabled] => Array
                                                                    (
                                                                        [0] => false
                                                                    )

                                                                [buyItNowAvailable] => Array
                                                                    (
                                                                        [0] => false
                                                                    )

                                                                [startTime] => Array
                                                                    (
                                                                        [0] => 2018-12-26T16:45:37.000Z
                                                                    )

                                                                [endTime] => Array
                                                                    (
                                                                        [0] => 2018-12-26T16:54:35.000Z
                                                                    )

                                                                [listingType] => Array
                                                                    (
                                                                        [0] => StoreInventory
                                                                    )

                                                                [gift] => Array
                                                                    (
                                                                        [0] => false
                                                                    )

                                                            )

                                                    )

                                                [returnsAccepted] => Array
                                                    (
                                                        [0] => true
                                                    )

                                                [isMultiVariationListing] => Array
                                                    (
                                                        [0] => false
                                                    )

                                                [topRatedListing] => Array
                                                    (
                                                        [0] => false
                                                    )

                                            )

1 个答案:

答案 0 :(得分:0)

您必须浏览阵列/响应,直到shippingServiceCost

$eBayResponse = "(assume it's the eBay API JSON response decoded and valid)";
$sum = 0;
foreach ( $eBayResponse['findCompletedItemsResponse'] as $completedItemsResponse ) {
    foreach ( $completedItemsResponse['searchResult'] as $searchResults ) {
        foreach ( $searchResults['item'] as $items ) {
            foreach ( $items['shippingInfo'] as $shippingInfos ) {
                foreach ( $shippingInfos['shippingServiceCost'] as $shippingServiceCost ) {
                    if ( isset($shippingServiceCost['__value__']) ) {
                        $sum += $shippingServiceCost['__value__'];
                    }
                }
            }
        }
    }
}
echo $sum; // 99.97 with the corrected sample