Php 如何重写/重新格式化数组?

Php 如何重写/重新格式化数组?,php,laravel,collections,Php,Laravel,Collections,我正在开发一个API,但我在将其连接到支付平台时遇到问题,因为他们要求使用以下格式的首选项: 'items' => [ [ 'id' => 1, 'title' => 'Product 1', 'description' => 'Some description', 'picture_url' => 'img.png', 'quan

我正在开发一个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
    }
所以我发送(这是错误的):

我如何重写或重新格式化:
json\u encode(new CartItemCollection($items))
以获取rigth数组

我需要这样做:

对于我收藏中的每一项(项目),请执行以下操作:ProductID(我的)重写为id(平台形式)、数量(我的)重写为数量(平台形式)、价格(我的)重写为单价(平台形式)等


提前感谢:)

从代码上看,您似乎正在为您的
CartItem
模型使用雄辩的API资源

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

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

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

从代码中,您似乎正在为
CartItem
模型使用雄辩的API资源

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

'items'=>(新CartItemCollection($items))->toArray()['items']
此代码可能需要进行一些调整,因为您没有发布
CartItemCollection的类代码以及用于生成当前获得的输出结构的其他相关代码。

根据,可以定义另一个集合资源类来定制响应

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,
        ];
    }
}
根据,可以使用另一种方法定义另一个集合资源类来定制响应

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,
        ];
    }
}

CartItemCollection是什么样子的?在将其传递给该类之前,您在该项目中得到了什么?@Christoff我的收藏看起来像是我发布的,我只是编辑了一个更具体的问题:“项目”:[{…@ZeljkoMiloradovic My$项目只是购物车中的一个项目数组,但这些项目没有价格(或者至少不像plataform预期的那样)问题是,我需要重新格式化整个集合,我在考虑可能使用foreach,并手动指定新的键和值,但我真的不知道该怎么做CartItemCollection是什么样子的?在将其传递给该类之前,您在该项中得到了什么?@Christoff我的集合看起来像是我发布的,我只是编辑它更具体地说,问题是:“物品”:[{…@ZeljkoMiloradovic my$物品只是购物车中的一系列物品,但这些物品没有价格(或者至少不像plataform预期的那样)问题是,我需要重新格式化整个集合,我在考虑可能使用foreach,并手动指定新的键和值,但我真的不知道该怎么做谢谢你的回答:)我只是想把原来的问题编辑得更具体一点,你需要我发布关于CartItemCollection的什么部分?是的,我使用的是雄辩的ahah很抱歉没有提到你可以尝试代码,因为它可能在任何情况下都会起作用,但请确保你也可以发布
CartItemCollection
CartItemResource
classe谢谢你的回答:)我只想把原始问题编辑得更具体一点,你需要我发布关于CartItemCollection的什么部分?是的,我使用的是雄辩的ahah抱歉没有提到你可以尝试代码,因为它可能会起作用,但为了确保你也可以发布
CartItemCollection
CartItemResource