尝试遍历我的 json 中的所有对象以获取 php 中的所有 amount_refunded
Trying to loop through all the objects in my json to get all the amount_refunded in php
这是我在邮递员中的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
假设您的 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;
您可以使用 Laravel 集合来获取 amount_refunded
的数组。
$refunds = collect(json_decode($response))
->flatten()
->pluck('amount_refunded')
->toArray();
获取按amount_refunded > 0
过滤的所有数据
$refunds = collect(json_decode($response))->flatten()->filter(function ($item) {
return $item->amount_refunded > 0;
})->toArray();
Docs: https://laravel.com/docs/8.x/collections#available-methods
假设您显示的 JSON 是 $response
的内容,JSON 中的任何地方都没有提到“项目”,所以不清楚您为什么要包含它或者为什么你认为它有效。如果您尝试使用您在该数据中显示的代码,肯定会导致错误/警告。
您的 JSON 结构是一个外部数组,内部有多个较小的数组 - 每个较小的数组都可以有一个或多个条目。
这个简单的代码将输出每个“amount_refunded”字段的值,通过循环外部数组,然后循环每个内部数组:
$refunds = json_decode($response);
foreach($refunds as $refund) {
foreach ($refund as $item)
echo $item->amount_refunded.PHP_EOL; //PHP_EOL just adds a newline character. Use <br/> instead for HTML output.
}
工作演示:http://sandbox.onlinephpfunctions.com/code/728b81aa408b811b9d98a0c01d3dc782c4f05951
这是我在邮递员中的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假设您的 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;
您可以使用 Laravel 集合来获取 amount_refunded
的数组。
$refunds = collect(json_decode($response))
->flatten()
->pluck('amount_refunded')
->toArray();
获取按amount_refunded > 0
$refunds = collect(json_decode($response))->flatten()->filter(function ($item) {
return $item->amount_refunded > 0;
})->toArray();
Docs: https://laravel.com/docs/8.x/collections#available-methods
假设您显示的 JSON 是 $response
的内容,JSON 中的任何地方都没有提到“项目”,所以不清楚您为什么要包含它或者为什么你认为它有效。如果您尝试使用您在该数据中显示的代码,肯定会导致错误/警告。
您的 JSON 结构是一个外部数组,内部有多个较小的数组 - 每个较小的数组都可以有一个或多个条目。
这个简单的代码将输出每个“amount_refunded”字段的值,通过循环外部数组,然后循环每个内部数组:
$refunds = json_decode($response);
foreach($refunds as $refund) {
foreach ($refund as $item)
echo $item->amount_refunded.PHP_EOL; //PHP_EOL just adds a newline character. Use <br/> instead for HTML output.
}
工作演示:http://sandbox.onlinephpfunctions.com/code/728b81aa408b811b9d98a0c01d3dc782c4f05951