重新排列数组索引雄辩的Laravel

编程入门 行业动态 更新时间:2024-10-07 10:21:00
本文介绍了重新排列数组索引雄辩的Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

从雄辩的laravel数组中删除元素后出现错误

I have an error after delete an element from an array of 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时,数组将转换为对象而不是数组

When i encode this into a json, the array is convert into an object not an array

未设置

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":""}}}

推荐答案

我在laravel文档中找到了一个解决方案.

I have found a solution in the laravel documentation.

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

after modifying the array, you have to call a method from laravel collection named values() that arrange the array indexes, example:

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

更多推荐

重新排列数组索引雄辩的Laravel

本文发布于:2023-11-29 15:05:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646744.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:雄辩   数组   排列   索引   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!