0

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

FranCode
  • 93
  • 1
  • 7
  • Possible duplicate of [PHP Sort a multidimensional array by element containing date](https://stackoverflow.com/questions/2910611/php-sort-a-multidimensional-array-by-element-containing-date) – BenRoob Sep 26 '17 at 13:55
  • EDIT: I don't need to sort $hlisting but only $hlisting->prices (that containes value and date) – FranCode Sep 26 '17 at 13:57
  • Sorry that's an object not an array – FranCode Sep 26 '17 at 14:01

1 Answers1

0

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.

Luke
  • 943
  • 6
  • 12