在嵌套对象中排序和显示数据-Laravel

时间:2019-07-10 12:03:29

标签: arrays object laravel-5

嗨,我有打印数组对象的问题。我想按整条记录排序,如预期的结果。

    object:
     "name":1,
     "id":1,
         "occ":  [{  
             "accomadates":1},

                 {  
             "accomadates":2}
     {  
             "accomadates":3}],
   "name":2,
     "id":2,
         "occ":  [{  
             "accomadates":2},

                 {  
             "accomadates":3}
                 }],

现在其打印效果如下:

id 1 :accomadates  1
id 1 :accomadates  2
id 1 :accomadates  3
id 2 :accomadates  2
id 2 :accomadates  3

预期结果:

id 1 :accomadates  1
id 2 :accomadates  1
id 1 :accomadates  2
id 1 :accomadates  3
id 2 :accomadates  3

我可以在Laravel做吗?请帮助我。谢谢大家

编辑:

//this is how i print using foreach loop

@foreach($ojects as $object)

   @foreach(object->occ as $occ)
      id {{$oject->name}} :accomadates  {{$occ->accomadates}} 
   @endforeach
<br>
@endforeach

我想要打印完全一样:

id 1 :accomadates  1
id 2 :accomadates  1
id 1 :accomadates  2
id 1 :accomadates  3
id 2 :accomadates  3

1 个答案:

答案 0 :(得分:0)

尝试使用将数组转换为Collection

$c = collect($c);

像这样按属性排序:

$sorted = $c->sortBy('id');

如果您希望结果为数组,请使用

$c = $c->toArray();

要将集合转换回数组