【问题标题】:Sort a multidimensional array by date Laravel按日期对多维数组排序 Laravel
【发布时间】:2018-03-07 19:02:55
【问题描述】:

我需要按日期对多维数组进行排序。 数据在名为价格的列中序列化,其中包含很少的值:价格和日期。 我在控制器中对它进行了反序列化,并且得到了 $hlisting->prices。 我认为这个价格是一个数组,我将按价格内的数据值对所有 $hlisting 进行排序。 (种类 $hlisting->prices->date)。

这是一个可能有用的代码。

<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 发送到视图的控制器中对其进行排序。

谢谢

【问题讨论】:

标签: php arrays laravel sorting multidimensional-array


【解决方案1】:

我认为最好在查询数据库时解决这个问题。在查询中,您可以使用 ORDER BY 语句以特定顺序(在您的情况下为日期和价格)提取行。调用多个 Order By 将分别按该顺序查询它们

在 Laravel 中:

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

或者一个通用的mysql查询字符串:

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

例如。

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

【讨论】:

    猜你喜欢
    • 2016-10-31
    • 2013-11-30
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    相关资源
    最近更新 更多