2014-12-05 36 views
3

我有一个错误之后从laravel口才重新排列的数组索引锋Laravel

的属性的数组中删除元件具有房间

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

请你能张贴$财产而>房间原来的PHP对象 – Gustonez 2014-12-05 07:07:07

回答

6

我发现laravel文档中的溶液中。

修改后阵,你必须调用从命名值laravel集合()的方法安排数组索引,例如:

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

你在文档中找到了这个地方? @Miguel – 2017-12-05 08:16:35

+0

https://laravel.com/docs/5.5/collections#method-values – Andy 2018-03-01 13:47:55

0

选项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); 

选项2

要从JSON转换为阵列设置ASSOC标志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);