简体   繁体   中英

Sort a multidimensional array by date Laravel

I need to sort a multidimensional array by date. The data is serialized in a column called prices that contains few values: prices and date. I unserialized this in my controller and I've got $hlisting->prices. I think that this prices is an array and I was to sort all $hlisting by that data value inside prices. (kind $hlisting->prices->date).

That's a code that may be helpful.

<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>

I think I should sort it in the controller that send $hlisting to the view.

Thanks

I think this problem is best solved when querying your database. In the query, you can use ORDER BY statements to extract your rows in a specific order (in your case date and price). Calling multiple Order By's will query them in that order respectively

In Laravel:

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

Or a generic mysql query string:

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

for example.

By doing this your $hlisting object/array will already be ordered appropriately and when calling it in your view, it will display ordered by date automatically and then by price.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM