【问题标题】:How can I rewrite/reformat an array?如何重写/重新格式化数组?
【发布时间】:2019-09-11 22:10:08
【问题描述】:

我正在开发 API,但在将其连接到支付平台时遇到问题,因为他们要求使用以下格式的首选项:

'items' => [
        [
            'id' => 1,
            'title' => 'Product 1',
            'description' => 'Some description',
            'picture_url' => 'img.png',
            'quantity' => 1,
            'currency_id' => 'USD',
            'unit_price' => 20
        ],

        [
            'id' => 2,
            'title' => 'Product 2',
            'description' => 'Some description',
            'picture_url' => 'img.png',
            'quantity' => 1,
            'currency_id' => 'USD',
            'unit_price' => 25
        ]
    ]

但我正在从购物车中的商品中接收数据,如下所示:

 'items' => json_encode(new CartItemCollection($items))

那个集合(我的集合 CartItemCollection)有这种格式:

 {
   "Items":[
              {
                "productID":1,
                "inventoryID":1,
                "name":"Product 1",
                "Quantity":1,
                "price":20,
                "image":"img.png"
              },

              {
                "productID":2,
                "inventoryID":1,
                "name":"Product2 "
                "Quantity":1,
                "price":25,
                "image":"img.png"
               }
            ],
        "items_count":2,
        "products_count":2
    }

所以我要发送(这是错误的):

'items' => "Items":[
              {
                "productID":1,
                "inventoryID":1,
                "name":"Product 1",
                "Quantity":1,
                "price":20,
                "image":"img.png"
              },

              {
                "productID":2,
                "inventoryID":1,
                "name":"Product2 "
                "Quantity":1,
                "price":25,
                "image":"img.png"
               }
            ],
        "items_count":2,
        "products_count":2

如何重写或重新格式化:json_encode(new CartItemCollection($items)) 以获得正确的数组?

我有点需要这样做:

foreach(项目)在我的收藏中,做:ProductID(我的)重写为 id(平台),数量(我的)重写为数量(平台),价格(我的)重写为 unit_price(平台)等等。

提前谢谢你:)

【问题讨论】:

  • CartItemCollection 是什么样的?
  • 在传递给那个类之前,你从这些项目中得到了什么?
  • @ChristoffX 我的收藏看起来像是我发布的,我只是将问题编辑得更具体,但它是:“Items”:[{...
  • @ZeljkoMiloradovic 我的 $items 只是购物车中的一系列商品,但这些商品没有价格(或至少不像平台预期的那样)
  • 问题是我需要重新格式化整个集合,我在想也许用一个 foreach,并分配新的键和值,比如手动,但我真的不知道我该怎么做

标签: php laravel collections


【解决方案1】:

根据the official documentation,另一种方法可以定义另一个集合资源类来自定义响应。

return new CustomCartItemCollection($items);

在 CustomCarItemCollection.php 类中:

use Illuminate\Http\Resources\Json\ResourceCollection;

class CustomCarItemCollection extends ResourceCollection
{
    public $collects = 'App\Http\Resources\Item';

    public function toArray($request)
    {
        return [
            'items' => $this->collection,
        ];
    }
}
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class Item extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->ProductID,
            'quantity' => $this->Quantity,
            'unit_price' => $this->Price,
        ];
    }
}

【讨论】:

    【解决方案2】:

    从代码看来,您正在为您的 CartItem 模型使用 Eloquent API 资源。

    如果正确,您不应该使用 json_encode,因为它会将您的对象转换为字符串,但您可以尝试直接调用 CartItemCollection 上的 toArray 方法:

    'items' => (new CartItemCollection($items))->toArray()['Items']
    

    此代码可能需要一些调整,因为您没有发布 CartItemCollection 的类代码以及用于生成您现在获得的输出结构的其他相关代码。

    【讨论】:

    • 感谢您的回答 :) 我只是将原始问题编辑得更具体一些,您需要我发布关于 CartItemCollection 的哪一部分?是的,我正在使用 Eloquent 啊抱歉没有提到这一点
    • 您可以尝试该代码,因为它可能仍然可以工作,但要确保您也可以发布 CartItemCollectionCartItemResource
    猜你喜欢
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    相关资源
    最近更新 更多