gpt4 book ai didi

php - 重新排列数组索引 Eloquent Laravel

转载 作者:可可西里 更新时间:2023-11-01 00:20:24 25 4
gpt4 key购买 nike

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

最佳答案

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

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

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

关于php - 重新排列数组索引 Eloquent Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27309747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com