Rearrange array index Eloquent Laravel

雨燕双飞 提交于 2019-12-05 18:56:17

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

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