1

bookedTimes are already showing my problem is how can I sort the items from the highest booked to the lest booked. Any answers and suggestions are welcome. Thank you in advance.

<div class="row">

    @if(!empty($Service) && count($Service))

    @foreach($Service['result'] as $value)

    @foreach($value as $val)
    <?php
        $arrange = array($val['bookedTimes']);
        arsort($arrange);
        foreach($arrange as $x => $x_value) {
            echo $x_value;
        }
    ?>

    @endforeach
    @endforeach
    @else
    <div class="col-lg-12 col-md-12"><h4>Data Not Found! </h4> </div>
    @endif
</div> 

This is the booked time data that is need to be in Descending Order

enter image description here

2
  • 1
    You can use orderBy in your controller and then pass it to your view. Commented Oct 29, 2020 at 7:04
  • @dqureshiumar How can I make it Commented Oct 29, 2020 at 7:11

2 Answers 2

3

Place an usort function after the first iteration's start point and you will get the sorted bookTimes in the second iteration.

<div class="row">

@if(!empty($Service) && count($Service))

@foreach($Service['result'] as $value)

<?php

usort( $value, function ( $a, $b )
{
//$b should come first as you want to sort in descending order
return $b['bookedTimes'] <=> $a['bookedTimes'];
});
?>

@foreach($value as $val)


@endforeach
@endforeach
@else
<div class="col-lg-12 col-md-12"><h4>Data Not Found! </h4> </div>
@endif
</div>
Sign up to request clarification or add additional context in comments.

9 Comments

Hi I got this error: Illegal string offset 'booked-times'
you have to change the booked-times string to name as name index is holding the values , replace your cmp function by mine
please , share your errors in post for which you are getting error in illegal offset
okay got it , you are sorting using the book-times not names .sorry for getting wrong. okay why don't sort from the eloquent using orderBy
|
1

arsort sorts an array into reverse order. It has two parameters. The second says how the elements of the array should be interpreted.

To ensure that you get a numeric sort you can use:

arsort($arrange, SORT_NUMERIC)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.