Codi
Codi

Reputation: 438

setting display results from json by id value returned

I have pulled my json results and using the following. but i need them to display to order by position 1.2.3 right now its 4321

 foreach($result['channels'] as $item){    

    $name = $item['name'];
    $position = $item['position'];
    $channel_id =  $item['id'];

    echo '<div class="channel">
            <div class="channel-name">'.$name.'</div>
          </div>';

}

Upvotes: 0

Views: 18

Answers (1)

Syscall
Syscall

Reputation: 19779

You could use usort() before to use the foreach() :

usort($result['channels'], function($a, $b) {
    return $a['position'] - $b['position'];
});

foreach($result['channels'] as $item){    

    $name = $item['name'];
    $position = $item['position'];
    $channel_id =  $item['id'];

    echo '<div class="channel">
            <div class="channel-name">'.$name.'</div>
          </div>';

}

Upvotes: 2

Related Questions