2017-09-26 172 views
0

我需要按日期排序多维数组。 数据在称为价格的列中序列化,该列包含几个值:价格和日期。 我在我的控制器中反序列化了这个,我有$ hlisting->价格。 我认为这个价格是一个数组,我是通过价格内的数据价值对所有的价格进行排序。 (kind $ hlisting-> prices-> date)。排序多维数组按日期Laravel

这可能是有益的代码。

<div class="price-dates"> 
     @if(!empty($hlisting->prices)) 
      @foreach($hlisting->prices as $prices) 
       <div class="price-date"> 
        <div class="row"> 
         <div class="col-sm-6"> 
          <div class="form-group"> 
           <label>Period</label> 
           {!! Form::text('prices_dates[]', $prices['date'], array('class' => 'date-range form-control')) !!} 
          </div> 
         </div> 
         <div class="col-sm-4"> 
          <div class="form-group"> 
           <label>Value</label> 
           {!! Form::text('prices_values[]', $prices['value'], array('class' => 'numeric form-control')) !!} 
          </div> 
         </div><!--col--> 
         <div class="col-sm-1"> 
          <div class="form-group"> 
           <label>Del</label> 
           <button type="button" class="del-price-date btn btn-sm btn-danger"> 
            <i class="fa fa-trash"></i> 
           </button> 
          </div> 
         </div> 
        </div><!--row--> 
       </div> 
      @endforeach 
     @else 
      <div class="price-date"> 
       <div class="row"> 
        <div class="col-sm-6"> 
         <div class="form-group"> 
          <label>Period</label> 
          {!! Form::text('prices_dates[]', NULL, array('class' => 'date-range form-control')) !!} 
         </div> 
        </div> 
        <div class="col-sm-4"> 
         <div class="form-group"> 
          <label>Value</label> 
          {!! Form::text('prices_values[]', NULL, array('class' => 'numeric form-control')) !!} 
         </div> 
        </div><!--col--> 
       </div><!--row--> 
      </div> 
     @endif 
    </div> 

我想我应该在发送$ hlisting到视图的控制器中进行排序。

感谢

+0

[PHP分类由含有日期元件多维数组(https://stackoverflow.com/questions/2910611/php-sort-a-multidimensional-array-by-element-containing-date) – BenRoob

+0

EDIT的可能的复制:我不需要排序$ hlisting但只有hlisting- $>价格(即containes值和日期) – FranCode

+0

对不起这是一个对象不是一个数组 – FranCode

回答

0

我想查询数据库时,这个问题最好解决。在查询中,您可以使用ORDER BY语句以特定顺序(以您的案例日期和价格)提取行。调用多个排序的将依次分别

查询他们在Laravel:

$hlisting = Model::where('x', $y)->OrderBy('date','asc')->OrderBy('price','asc')->get();

或通用MySQL查询字符串:

SELECT * FROM table WHERE x = $y ORDER BY date ASC ORDER BY price ASC

例如。

通过这样做,你的$ hlisting对象/数组就已经被订购适当并在您的视图调用它时,它会显示自动按日期,然后订购的价格。