首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于移除数组中对象的array_pop

用于移除数组中对象的array_pop
EN

Stack Overflow用户
提问于 2021-08-02 11:07:59
回答 1查看 45关注 0票数 0

我有一个要从其中获取数组的类。

代码语言:javascript
复制
$attributeTypeOfProducts= $Attributetypes::with('cars')->get();

我想使用array_pop方法删除数组的最后一条记录,但它给出了以下错误

代码语言:javascript
复制
Indirect modification of overloaded element of App\\Models\\Attributetypes has no effect

我做了下面的

代码语言:javascript
复制
$pc=0;
$popcount =0;
for( $i = 0; $i < count($attributeTypeOfProducts); $i++){ 

 for($pc=0; $pc<5; $pc++)
             {
                 
               
                array_pop($attributeTypeOfProducts[$i]['cars']);
               
             }



}


                    Response of the class

{
    "id": 1,
    "name": "cars",
    "car_id": 123443221,
    "status": 1,
    "created_at": "2021-08-02T05:53:49.000000Z",
    "updated_at": "2021-08-02T05:53:49.000000Z",
    "cars": [
        {
            "id": 3,
            "attr_size_value": 14,
            "attr_image_src": "abc.png",
            "attribute_size_name": "honda",
            "attribute_type_id": 1,
            "product_id": 123443221,
            "created_at": "2021-08-02T05:53:50.000000Z",
            "updated_at": "2021-08-02T05:53:50.000000Z"
        },
        {
            "id": 4,
            "attr_size_value": 10,
            "attr_image_src": "abc.png",
            "attribute_size_name": "bullet",
            "attribute_type_id": 2,
            "product_id": 123443221,
            "created_at": "2021-08-02T05:54:28.000000Z",
            "updated_at": "2021-08-02T05:54:28.000000Z"
        },
        {
            "id": 8,
            "attr_size_value": 12,
            "attr_image_src": "abc.png",
            "attribute_size_name": "hyundai",
            "attribute_type_id": 3,
            "product_id": 123443221,
            "created_at": "2021-08-02T05:55:02.000000Z",
            "updated_at": "2021-08-02T05:55:02.000000Z"
        },
        {
            "id": 9,
            "attr_size_value": 14,
            "attr_image_src": "abc.png",
            "attribute_size_name": "city",
            "attribute_type_id": 3,
            "product_id": 123443221,
            "created_at": "2021-08-02T05:55:02.000000Z",
            "updated_at": "2021-08-02T05:55:02.000000Z"
        }
    ],
    
}

我想删除带有attribute_size_name = city的记录,但它在array_pop上崩溃了

我有一个cars关系,我想删除它的最后一条记录,但它给出了前面提到的错误

执行此$Attributetypes::with('cars')->get()->toArray();操作后,我无法将另一个阵列推送到$attributeTypeOfProducts中

EN

回答 1

Stack Overflow用户

发布于 2021-08-02 11:39:05

您可以通过使用toArray()方法将集合转换为数组来实现您的结果:

代码语言:javascript
复制
$attributeTypeOfProducts= $Attributetypes::with('cars')->get()->toArray();

您也可以使用laravel集合中的方法来完成此操作:

代码语言:javascript
复制
$attributeTypeOfProducts= $Attributetypes::with('cars')->get();

$attributeTypeOfProducts->map(function($item, $key){
    if($key < 5){
        array_pop($item['cars']);
        return $item;
    }
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68620549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档