首页 > 解决方案 > 试图遍历我的 json 中的所有对象以在 php 中获取所有的 amount_refunded

问题描述

这是我在邮递员中的 json:

     [
    [
        {
            "amount_refunded": 1,
            "base_amount_refunded": 1,
            "base_discount_amount": 0,
            "base_discount_invoiced": 0,
            "base_discount_refunded": 0,
            "base_discount_tax_compensation_amount": 0,
            "base_discount_tax_compensation_invoiced": 0,
            "base_discount_tax_compensation_refunded": 0,
            "base_original_price": 1,
            "base_price": 1,
            "base_price_incl_tax": 1,
            "base_row_invoiced": 1,
            "base_row_total": 1,
            "base_row_total_incl_tax": 1,
            "base_tax_amount": 0,
            "base_tax_invoiced": 0,
            "base_tax_refunded": 0,
            "created_at": "2021-10-04 13:02:36",
            "discount_amount": 0,
            "discount_invoiced": 0,
            "discount_percent": 0,
            "discount_refunded": 0,
            "free_shipping": 0,
            "discount_tax_compensation_amount": 0,
            "discount_tax_compensation_invoiced": 0,
            "discount_tax_compensation_refunded": 0,
            "is_qty_decimal": 0,
            "is_virtual": 0,
            "item_id": 166,
            "name": "Configurable Product 16 - option 21",
            "no_discount": 0,
            "order_id": 85,
            "original_price": 1,
            "price": 1,
            "price_incl_tax": 1,
            "product_id": 1234,
            "product_type": "simple",
            "qty_canceled": 0,
            "qty_invoiced": 1,
            "qty_ordered": 1,
            "qty_refunded": 1,
            "qty_shipped": 1,
            "quote_item_id": 164,
            "row_invoiced": 1,
            "row_total": 1,
            "row_total_incl_tax": 1,
            "row_weight": 1,
            "sku": "Configurable Product 16 - option 21",
            "store_id": 1,
            "tax_amount": 0,
            "tax_invoiced": 0,
            "tax_percent": 0,
            "tax_refunded": 0,
            "updated_at": "2021-10-15 09:28:44",
            "weee_tax_applied": "[]",
            "weight": 1
        }
    ],
    [
        {
            "amount_refunded": 80,
            "applied_rule_ids": "1",
            "base_amount_refunded": 80,
            "base_discount_amount": 8,
            "base_discount_invoiced": 8,
            "base_discount_refunded": 8,
            "base_discount_tax_compensation_amount": 0,
            "base_discount_tax_compensation_invoiced": 0,
            "base_discount_tax_compensation_refunded": 0,
            "base_original_price": 80,
            "base_price": 80,
            "base_price_incl_tax": 86.6,
            "base_row_invoiced": 80,
            "base_row_total": 72,
            "base_row_total_incl_tax": 77.94,
            "base_tax_amount": 5.94,
            "base_tax_invoiced": 5.94,
            "base_tax_refunded": 5.94,
            "created_at": "2021-10-15 11:23:54",
            "discount_amount": 8,
            "discount_invoiced": 8,
            "discount_percent": 10,
            "discount_refunded": 8,
            "free_shipping": 0,
            "discount_tax_compensation_amount": 0,
            "discount_tax_compensation_invoiced": 0,
            "discount_tax_compensation_refunded": 0,
            "is_qty_decimal": 0,
            "is_virtual": 0,
            "item_id": 169,
            "name": "Ski vest 92",
            "no_discount": 0,
            "order_id": 88,
            "original_price": 80,
            "price": 80,
            "price_incl_tax": 86.6,
            "product_id": 1267,
            "product_type": "simple",
            "qty_canceled": 0,
            "qty_invoiced": 1,
            "qty_ordered": 1,
            "qty_refunded": 1,
            "qty_shipped": 1,
            "quote_item_id": 171,
            "row_invoiced": 80,
            "row_total": 72,
            "row_total_incl_tax": 77.94,
            "row_weight": 5,
            "sku": "Ski vest 92",
            "store_id": 1,
            "tax_amount": 5.94,
            "tax_invoiced": 5.94,
            "tax_percent": 8.25,
            "tax_refunded": 5.94,
            "updated_at": "2021-10-15 11:25:07",
            "weee_tax_applied": "[]",
            "weight": 5
        },
        {
            "amount_refunded": 0,
            "applied_rule_ids": "1",
            "base_amount_refunded": 0,
            "base_discount_amount": 2,
            "base_discount_invoiced": 2,
            "base_discount_tax_compensation_amount": 0,
            "base_discount_tax_compensation_invoiced": 0,
            "base_original_price": 20,
            "base_price": 20,
            "base_price_incl_tax": 20,
            "base_row_invoiced": 20,
            "base_row_total": 18,
            "base_row_total_incl_tax": 18,
            "base_tax_amount": 0,
            "base_tax_invoiced": 0,
            "created_at": "2021-10-15 11:23:54",
            "discount_amount": 2,
            "discount_invoiced": 2,
            "discount_percent": 10,
            "free_shipping": 0,
            "discount_tax_compensation_amount": 0,
            "discount_tax_compensation_invoiced": 0,
            "is_qty_decimal": 0,
            "is_virtual": 0,
            "item_id": 170,
            "name": "testcron",
            "no_discount": 0,
            "order_id": 88,
            "original_price": 20,
            "price": 20,
            "price_incl_tax": 20,
            "product_id": 1260,
            "product_type": "simple",
            "qty_canceled": 0,
            "qty_invoiced": 1,
            "qty_ordered": 1,
            "qty_refunded": 0,
            "qty_shipped": 1,
            "quote_item_id": 173,
            "row_invoiced": 20,
            "row_total": 18,
            "row_total_incl_tax": 18,
            "row_weight": 2,
            "sku": "testcron",
            "store_id": 1,
            "tax_amount": 0,
            "tax_invoiced": 0,
            "tax_percent": 0,
            "updated_at": "2021-10-15 11:24:39",
            "weee_tax_applied": "[]",
            "weight": 2
        },
        {
            "amount_refunded": 0,
            "applied_rule_ids": "1",
            "base_amount_refunded": 0,
            "base_discount_amount": 0.5,
            "base_discount_invoiced": 0.5,
            "base_discount_tax_compensation_amount": 0,
            "base_discount_tax_compensation_invoiced": 0,
            "base_original_price": 5,
            "base_price": 5,
            "base_price_incl_tax": 5.41,
            "base_row_invoiced": 5,
            "base_row_total": 4.5,
            "base_row_total_incl_tax": 4.87,
            "base_tax_amount": 0.37,
            "base_tax_invoiced": 0.37,
            "created_at": "2021-10-15 11:23:54",
            "discount_amount": 0.5,
            "discount_invoiced": 0.5,
            "discount_percent": 10,
            "free_shipping": 0,
            "discount_tax_compensation_amount": 0,
            "discount_tax_compensation_invoiced": 0,
            "is_qty_decimal": 0,
            "is_virtual": 0,
            "item_id": 171,
            "name": "time",
            "no_discount": 0,
            "order_id": 88,
            "original_price": 5,
            "price": 5,
            "price_incl_tax": 5.41,
            "product_id": 1261,
            "product_type": "simple",
            "qty_canceled": 0,
            "qty_invoiced": 1,
            "qty_ordered": 1,
            "qty_refunded": 0,
            "qty_shipped": 1,
            "quote_item_id": 174,
            "row_invoiced": 5,
            "row_total": 4.5,
            "row_total_incl_tax": 4.87,
            "row_weight": 2,
            "sku": "time",
            "store_id": 1,
            "tax_amount": 0.37,
            "tax_invoiced": 0.37,
            "tax_percent": 8.25,
            "updated_at": "2021-10-15 11:24:39",
            "weee_tax_applied": "[]",
            "weight": 2
        }
    ],
    [
        {
            "amount_refunded": 80,
            "base_amount_refunded": 80,
            "base_discount_amount": 0,
            "base_discount_invoiced": 0,
            "base_discount_refunded": 0,
            "base_discount_tax_compensation_amount": 0,
            "base_discount_tax_compensation_invoiced": 0,
            "base_discount_tax_compensation_refunded": 0,
            "base_original_price": 80,
            "base_price": 80,
            "base_price_incl_tax": 80,
            "base_row_invoiced": 80,
            "base_row_total": 80,
            "base_row_total_incl_tax": 80,
            "base_tax_amount": 0,
            "base_tax_invoiced": 0,
            "base_tax_refunded": 0,
            "created_at": "2021-10-19 08:49:35",
            "discount_amount": 0,
            "discount_invoiced": 0,
            "discount_percent": 0,
            "discount_refunded": 0,
            "free_shipping": 0,
            "discount_tax_compensation_amount": 0,
            "discount_tax_compensation_invoiced": 0,
            "discount_tax_compensation_refunded": 0,
            "is_qty_decimal": 0,
            "is_virtual": 0,
            "item_id": 172,
            "name": "Ski vest 92",
            "no_discount": 0,
            "order_id": 89,
            "original_price": 80,
            "price": 80,
            "price_incl_tax": 80,
            "product_id": 1267,
            "product_type": "simple",
            "qty_canceled": 0,
            "qty_invoiced": 1,
            "qty_ordered": 1,
            "qty_refunded": 1,
            "qty_shipped": 1,
            "quote_item_id": 175,
            "row_invoiced": 80,
            "row_total": 80,
            "row_total_incl_tax": 80,
            "row_weight": 5,
            "sku": "Ski vest 92",
            "store_id": 1,
            "tax_amount": 0,
            "tax_invoiced": 0,
            "tax_percent": 0,
            "tax_refunded": 0,
            "updated_at": "2021-10-19 09:06:19",
            "weee_tax_applied": "[]",
            "weight": 5
        },
        {
            "amount_refunded": 5,
            "base_amount_refunded": 5,
            "base_discount_amount": 0,
            "base_discount_invoiced": 0,
            "base_discount_refunded": 0,
            "base_discount_tax_compensation_amount": 0,
            "base_discount_tax_compensation_invoiced": 0,
            "base_discount_tax_compensation_refunded": 0,
            "base_original_price": 5,
            "base_price": 5,
            "base_price_incl_tax": 5,
            "base_row_invoiced": 5,
            "base_row_total": 5,
            "base_row_total_incl_tax": 5,
            "base_tax_amount": 0,
            "base_tax_invoiced": 0,
            "base_tax_refunded": 0,
            "created_at": "2021-10-19 08:49:35",
            "discount_amount": 0,
            "discount_invoiced": 0,
            "discount_percent": 0,
            "discount_refunded": 0,
            "free_shipping": 0,
            "discount_tax_compensation_amount": 0,
            "discount_tax_compensation_invoiced": 0,
            "discount_tax_compensation_refunded": 0,
            "is_qty_decimal": 0,
            "is_virtual": 0,
            "item_id": 173,
            "name": "time",
            "no_discount": 0,
            "order_id": 89,
            "original_price": 5,
            "price": 5,
            "price_incl_tax": 5,
            "product_id": 1261,
            "product_type": "simple",
            "qty_canceled": 0,
            "qty_invoiced": 1,
            "qty_ordered": 1,
            "qty_refunded": 1,
            "qty_shipped": 1,
            "quote_item_id": 177,
            "row_invoiced": 5,
            "row_total": 5,
            "row_total_incl_tax": 5,
            "row_weight": 2,
            "sku": "time",
            "store_id": 1,
            "tax_amount": 0,
            "tax_invoiced": 0,
            "tax_percent": 0,
            "tax_refunded": 0,
            "updated_at": "2021-10-19 09:06:19",
            "weee_tax_applied": "[]",
            "weight": 2
        }
    ],
    [
        {
            "amount_refunded": 5,
            "base_amount_refunded": 5,
            "base_discount_amount": 0,
            "base_discount_invoiced": 0,
            "base_discount_refunded": 0,
            "base_discount_tax_compensation_amount": 0,
            "base_discount_tax_compensation_invoiced": 0,
            "base_discount_tax_compensation_refunded": 0,
            "base_original_price": 5,
            "base_price": 5,
            "base_price_incl_tax": 5.41,
            "base_row_invoiced": 5,
            "base_row_total": 5,
            "base_row_total_incl_tax": 5.41,
            "base_tax_amount": 0.41,
            "base_tax_invoiced": 0.41,
            "base_tax_refunded": 0.41,
            "created_at": "2021-10-19 10:27:53",
            "discount_amount": 0,
            "discount_invoiced": 0,
            "discount_percent": 0,
            "discount_refunded": 0,
            "free_shipping": 0,
            "discount_tax_compensation_amount": 0,
            "discount_tax_compensation_invoiced": 0,
            "discount_tax_compensation_refunded": 0,
            "is_qty_decimal": 0,
            "is_virtual": 0,
            "item_id": 176,
            "name": "Configurable Product 16",
            "no_discount": 0,
            "order_id": 91,
            "original_price": 5,
            "price": 5,
            "price_incl_tax": 5.41,
            "product_id": 1253,
            "product_type": "configurable",
            "qty_canceled": 0,
            "qty_invoiced": 1,
            "qty_ordered": 1,
            "qty_refunded": 1,
            "qty_shipped": 1,
            "quote_item_id": 180,
            "row_invoiced": 5,
            "row_total": 5,
            "row_total_incl_tax": 5.41,
            "row_weight": 1,
            "sku": "Configurable Product 16 - option 9",
            "store_id": 1,
            "tax_amount": 0.41,
            "tax_invoiced": 0.41,
            "tax_percent": 8.25,
            "tax_refunded": 0.41,
            "updated_at": "2021-10-19 10:50:02",
            "weee_tax_applied": "[]",
            "weight": 1,
            "product_option": {
                "extension_attributes": {
                    "configurable_item_options": [
                        {
                            "option_id": "212",
                            "option_value": 478
                        },
                        {
                            "option_id": "213",
                            "option_value": 480
                        }
                    ]
                }
            }
        },
        {
            "amount_refunded": 0,
            "base_amount_refunded": 0,
            "base_discount_amount": 0,
            "base_discount_invoiced": 0,
            "base_discount_refunded": 0,
            "base_discount_tax_compensation_invoiced": 0,
            "base_discount_tax_compensation_refunded": 0,
            "base_price": 0,
            "base_row_invoiced": 0,
            "base_row_total": 0,
            "base_tax_amount": 0,
            "base_tax_invoiced": 0,
            "base_tax_refunded": 0,
            "created_at": "2021-10-19 10:27:53",
            "discount_amount": 0,
            "discount_invoiced": 0,
            "discount_percent": 0,
            "discount_refunded": 0,
            "free_shipping": 0,
            "discount_tax_compensation_invoiced": 0,
            "discount_tax_compensation_refunded": 0,
            "is_qty_decimal": 0,
            "is_virtual": 0,
            "item_id": 177,
            "name": "Configurable Product 16 - option 9",
            "no_discount": 0,
            "order_id": 91,
            "original_price": 0,
            "parent_item_id": 176,
            "price": 0,
            "product_id": 1222,
            "product_type": "simple",
            "qty_canceled": 0,
            "qty_invoiced": 1,
            "qty_ordered": 1,
            "qty_refunded": 1,
            "qty_shipped": 0,
            "quote_item_id": 181,
            "row_invoiced": 0,
            "row_total": 0,
            "row_weight": 0,
            "sku": "Configurable Product 16 - option 9",
            "store_id": 1,
            "tax_amount": 0,
            "tax_invoiced": 0,
            "tax_percent": 0,
            "tax_refunded": 0,
            "updated_at": "2021-10-19 10:50:02",
            "weight": 1,
            "parent_item": {
                "amount_refunded": 5,
                "base_amount_refunded": 5,
                "base_discount_amount": 0,
                "base_discount_invoiced": 0,
                "base_discount_refunded": 0,
                "base_discount_tax_compensation_amount": 0,
                "base_discount_tax_compensation_invoiced": 0,
                "base_discount_tax_compensation_refunded": 0,
                "base_original_price": 5,
                "base_price": 5,
                "base_price_incl_tax": 5.41,
                "base_row_invoiced": 5,
                "base_row_total": 5,
                "base_row_total_incl_tax": 5.41,
                "base_tax_amount": 0.41,
                "base_tax_invoiced": 0.41,
                "base_tax_refunded": 0.41,
                "created_at": "2021-10-19 10:27:53",
                "discount_amount": 0,
                "discount_invoiced": 0,
                "discount_percent": 0,
                "discount_refunded": 0,
                "free_shipping": 0,
                "discount_tax_compensation_amount": 0,
                "discount_tax_compensation_invoiced": 0,
                "discount_tax_compensation_refunded": 0,
                "is_qty_decimal": 0,
                "is_virtual": 0,
                "item_id": 176,
                "name": "Configurable Product 16",
                "no_discount": 0,
                "order_id": 91,
                "original_price": 5,
                "price": 5,
                "price_incl_tax": 5.41,
                "product_id": 1253,
                "product_type": "configurable",
                "qty_canceled": 0,
                "qty_invoiced": 1,
                "qty_ordered": 1,
                "qty_refunded": 1,
                "qty_shipped": 1,
                "quote_item_id": 180,
                "row_invoiced": 5,
                "row_total": 5,
                "row_total_incl_tax": 5.41,
                "row_weight": 1,
                "sku": "Configurable Product 16 - option 9",
                "store_id": 1,
                "tax_amount": 0.41,
                "tax_invoiced": 0.41,
                "tax_percent": 8.25,
                "tax_refunded": 0.41,
                "updated_at": "2021-10-19 10:50:02",
                "weee_tax_applied": "[]",
                "weight": 1,
                "product_option": {
                    "extension_attributes": {
                        "configurable_item_options": [
                            {
                                "option_id": "212",
                                "option_value": 478
                            },
                            {
                                "option_id": "213",
                                "option_value": 480
                            }
                        ]
                    }
                }
            },
            "row_total_incl_tax": 0,
            "base_row_total_incl_tax": 0
        }
    ]
]

我正在尝试遍历此 json,以便获得所有“amount_refunded 值”。这是我现在拥有的代码,但我不明白如何获得 amount_refunded 值。

请注意,使用 ->items 并且是正确的,因为它需要获取所有项目。

public function getRefundedOrders(Request $request)
{
    try{
        $response = Http::withToken($request->post('token'))
        ->get($request->post('URL').'/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=base_subtotal_refunded&searchCriteria[filter_groups][0][filters][0][condition_type]=gt');

    }
    catch(Throwaway $e){
        Log::error($e);
    }


    $refunds = json_decode($response);

    $user = Users::whereUrl($request->post('URL'))->firstOrFail();

    $refundedItems = [];


    foreach($refunds->items as $product){
        array_push($refundedItems, $product->items);
    }

    $refundedProducts = [];


    if (isset($response['message']))
    {
        return response()->json(['type' => 'error', 'message' => $response['message']]);
    }
    
    return $refundedItems;
   
}

我不明白如何循环遍历这个,所以我可以从这个 json 中的每个对象中获取所有的 amount_refunded。

标签: phpjsonlaravelloopserror-handling

解决方案


假设您的 json 缺少结束“]”,然后使用以下方法将其解码为关联数组:

$string = '--yourjsongoeshere--';
$data = json_decode($string, true);

然后将您的总退款设置为 0,遍历数组并将总退款添加到其中。

$totalrefund = 0;
foreach($data as $datum) { 
    foreach($datum as $entry) { 
        $totalrefund += $entry['amount_refunded'];
    }
}
echo $totalrefund;

推荐阅读