重新排列数组索引 Eloquent Laravel

Posted

技术标签:

【中文标题】重新排列数组索引 Eloquent Laravel【英文标题】:Rearrange array index Eloquent Laravel 【发布时间】:2015-02-03 06:15:18 【问题描述】:

从 laravel eloquent 数组中删除元素后出现错误

房产有房间

foreach ($property->rooms as $key => $room) 
    if ($room->type == 1 and $type ==1 and $room->price < $price->min or $room->price > $price->max) 
            print_r($property->rooms);
            unset($property->rooms[$key]);
            //$array = array_values($property->rooms); doesn't work
            print_r($property->rooms);
        
    

当我将其编码为 json 时,数组将转换为对象而不是数组

取消设置之前

Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
    (
        [0] => Room Object
            (
                [table:protected] => rooms
                [fillable:protected] => Array
                    (
                        [0] => properties_id
                        [1] => price
                        [2] => is_available
                        [3] => type
                        [4] => description
                    )

                [connection:protected] => 
                [primaryKey:protected] => id
                [perPage:protected] => 15
                [incrementing] => 1
                [timestamps] => 1
                [attributes:protected] => Array
                    (
                        [id] => 1
                        [properties_id] => 4
                        [price] => 450000
                        [is_available] => 1
                        [created_at] => 0000-00-00 00:00:00
                        [updated_at] => 2014-10-20 22:27:44
                        [type] => 1
                        [description] => 
                    )

                [original:protected] => Array
                    (
                        [id] => 1
                        [properties_id] => 4
                        [price] => 450000
                        [is_available] => 1
                        [created_at] => 0000-00-00 00:00:00
                        [updated_at] => 2014-10-20 22:27:44
                        [type] => 1
                        [description] => 
                    )

                [relations:protected] => Array
                    (
                    )

                [hidden:protected] => Array
                    (
                    )

                [visible:protected] => Array
                    (
                    )

                [appends:protected] => Array
                    (
                    )

                [guarded:protected] => Array
                    (
                        [0] => *
                    )

                [dates:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [with:protected] => Array
                    (
                    )

                [morphClass:protected] => 
                [exists] => 1
            )

        [1] => Room Object
            (
                [table:protected] => rooms
                [fillable:protected] => Array
                    (
                        [0] => properties_id
                        [1] => price
                        [2] => is_available
                        [3] => type
                        [4] => description
                    )

                [connection:protected] => 
                [primaryKey:protected] => id
                [perPage:protected] => 15
                [incrementing] => 1
                [timestamps] => 1
                [attributes:protected] => Array
                    (
                        [id] => 2
                        [properties_id] => 4
                        [price] => 350000
                        [is_available] => 1
                        [created_at] => 0000-00-00 00:00:00
                        [updated_at] => 2014-10-21 18:13:15
                        [type] => 1
                        [description] => 
                    )

                [original:protected] => Array
                    (
                        [id] => 2
                        [properties_id] => 4
                        [price] => 350000
                        [is_available] => 1
                        [created_at] => 0000-00-00 00:00:00
                        [updated_at] => 2014-10-21 18:13:15
                        [type] => 1
                        [description] => 
                    )

                [relations:protected] => Array
                    (
                    )

                [hidden:protected] => Array
                    (
                    )

                [visible:protected] => Array
                    (
                    )

                [appends:protected] => Array
                    (
                    )

                [guarded:protected] => Array
                    (
                        [0] => *
                    )

                [dates:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [with:protected] => Array
                    (
                    )

                [morphClass:protected] => 
                [exists] => 1
            )

        [2] => Room Object
            (
                [table:protected] => rooms
                [fillable:protected] => Array
                    (
                        [0] => properties_id
                        [1] => price
                        [2] => is_available
                        [3] => type
                        [4] => description
                    )

                [connection:protected] => 
                [primaryKey:protected] => id
                [perPage:protected] => 15
                [incrementing] => 1
                [timestamps] => 1
                [attributes:protected] => Array
                    (
                        [id] => 3
                        [properties_id] => 4
                        [price] => 250000
                        [is_available] => 1
                        [created_at] => 0000-00-00 00:00:00
                        [updated_at] => 0000-00-00 00:00:00
                        [type] => 1
                        [description] => 
                    )

                [original:protected] => Array
                    (
                        [id] => 3
                        [properties_id] => 4
                        [price] => 250000
                        [is_available] => 1
                        [created_at] => 0000-00-00 00:00:00
                        [updated_at] => 0000-00-00 00:00:00
                        [type] => 1
                        [description] => 
                    )

                [relations:protected] => Array
                    (
                    )

                [hidden:protected] => Array
                    (
                    )

                [visible:protected] => Array
                    (
                    )

                [appends:protected] => Array
                    (
                    )

                [guarded:protected] => Array
                    (
                        [0] => *
                    )

                [dates:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [with:protected] => Array
                    (
                    )

                [morphClass:protected] => 
                [exists] => 1
            )

    )

)

取消设置后

Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
    (
        [1] => Room Object
            (
                [table:protected] => rooms
                [fillable:protected] => Array
                    (
                        [0] => properties_id
                        [1] => price
                        [2] => is_available
                        [3] => type
                        [4] => description
                    )

                [connection:protected] => 
                [primaryKey:protected] => id
                [perPage:protected] => 15
                [incrementing] => 1
                [timestamps] => 1
                [attributes:protected] => Array
                    (
                        [id] => 2
                        [properties_id] => 4
                        [price] => 350000
                        [is_available] => 1
                        [created_at] => 0000-00-00 00:00:00
                        [updated_at] => 2014-10-21 18:13:15
                        [type] => 1
                        [description] => 
                    )

                [original:protected] => Array
                    (
                        [id] => 2
                        [properties_id] => 4
                        [price] => 350000
                        [is_available] => 1
                        [created_at] => 0000-00-00 00:00:00
                        [updated_at] => 2014-10-21 18:13:15
                        [type] => 1
                        [description] => 
                    )

                [relations:protected] => Array
                    (
                    )

                [hidden:protected] => Array
                    (
                    )

                [visible:protected] => Array
                    (
                    )

                [appends:protected] => Array
                    (
                    )

                [guarded:protected] => Array
                    (
                        [0] => *
                    )

                [dates:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [with:protected] => Array
                    (
                    )

                [morphClass:protected] => 
                [exists] => 1
            )

        [2] => Room Object
            (
                [table:protected] => rooms
                [fillable:protected] => Array
                    (
                        [0] => properties_id
                        [1] => price
                        [2] => is_available
                        [3] => type
                        [4] => description
                    )

                [connection:protected] => 
                [primaryKey:protected] => id
                [perPage:protected] => 15
                [incrementing] => 1
                [timestamps] => 1
                [attributes:protected] => Array
                    (
                        [id] => 3
                        [properties_id] => 4
                        [price] => 250000
                        [is_available] => 1
                        [created_at] => 0000-00-00 00:00:00
                        [updated_at] => 0000-00-00 00:00:00
                        [type] => 1
                        [description] => 
                    )

                [original:protected] => Array
                    (
                        [id] => 3
                        [properties_id] => 4
                        [price] => 250000
                        [is_available] => 1
                        [created_at] => 0000-00-00 00:00:00
                        [updated_at] => 0000-00-00 00:00:00
                        [type] => 1
                        [description] => 
                    )

                [relations:protected] => Array
                    (
                    )

                [hidden:protected] => Array
                    (
                    )

                [visible:protected] => Array
                    (
                    )

                [appends:protected] => Array
                    (
                    )

                [guarded:protected] => Array
                    (
                        [0] => *
                    )

                [dates:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [with:protected] => Array
                    (
                    )

                [morphClass:protected] => 
                [exists] => 1
            )

    )

)

转换成 json 的 rooms 字段应该是数组而不是对象:

"other_parameters": "something","rooms":"1":"id":2,"properties_id":4,"price":350000,"is_available":1,"created_at":"-0001-11-30 00:00:00","updated_at":"2014-10-21 18:13:15","type":1,"description":null,"2":"id":3,"properties_id":4,"price":250000,"is_available":1,"created_at":"-0001-11-30 00:00:00","updated_at":"-0001-11-30 00:00:00","type":1,"description":""

【问题讨论】:

请您发布 $property->rooms 的原始 php 对象 【参考方案1】:

我在 laravel 文档中找到了解决方案。

修改数组后,你必须从 laravel 集合中调用一个名为 values() 的方法来排列数组索引,例如:

unset($property->rooms[$key]);
$property->rooms->values();

【讨论】:

您在文档中哪里找到的? @米格尔【参考方案2】:

选项 1

$array = array();
foreach ($property->rooms as $key => $room) 
    if ($room->type == 1) 
        unset($property->rooms->$key); <<-- pay attention to this
     else 
        $array[] = get_object_vars($room); // sort the passed row into new array
    

print_r($array);

选项 2

要将 JSON 转换为数组,请将 assoc 标志设置为 TRUE

json_decode('Your json script', true);

foreach ($property['rooms'] as $key => $room) 
    if ($room['type'] == 1) 
        unset($property['rooms'][$key]);
    

$array = array_values($property['rooms']); 
print_r($array);

【讨论】:

以上是关于重新排列数组索引 Eloquent Laravel的主要内容,如果未能解决你的问题,请参考以下文章

如何通过索引将项目添加到 Laravel Eloquent 集合中?

遍历数组集合并返回命名键值对(Laravel/Eloquent)

Laravel:使用 unset() 后无法在急切加载中重新索引集合的数组

Laravel:将 Eloquent 集合转换为数组而不转换元素

Laravel Eloquent:返回数组键作为字段 ID

仅当一个字段作为数组提供时,Laravel Eloquent 才插入多条记录