Rearrange array index Eloquent Laravel

匿名 (未验证) 提交于 2019-12-03 08:56:10

问题:

I have an error after delete an element from an array of laravel eloquent

A property has rooms

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);         }     } 

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

Before unset

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             )      ) 

)

After unset

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             )      ) 

)

converted into a json the rooms field should be an array not an object:

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

回答1:

I have found a solution in the laravel documentation.

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(); 


回答2:

Option 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); 

Option 2

To convert from JSON to Array set the assoc flag to 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); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!