php将数组的索引重新排序,php – 重新排列数组索引Eloquent Laravel

从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,房间字段应该是一个数组而不是一个对象:

{"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文档中找到了一个解决方案.

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

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

$property->rooms->values();

标签:unset,php,arrays,laravel,json

来源: https://codeday.me/bug/20190824/1712600.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值