如何在PHP中动态排序HTML div?

I have 4 HTML divs (with nested divs) and sort numbers for each of the divs that can be returned from a function. I'd like to sort the divs based on the sort numbers returned from the function.

What's the best way to do this? I am thinking of arranging the divs in an array like this:

$array[$sort_order] = $div;

which will translate to

$array[1] = "<div class='container'><div class='caption'>One</div><div class='text'>....</div></div>";
$array[2] = "<div class='container'><div class='caption'>Two</div><div class='text'>....</div></div>";
$array[3] = "<div class='container'><div class='caption'>Three</div><div class='text'>....</div></div>";
$array[4] = "<div class='container'><div class='caption'>Four</div><div class='text'>....</div></div>";

and then use ksort() to sort them before displaying. I was wondering if there is a better way to do this?

i think, u need use ksort before displaying

I try to avoid ksort if I have to use loop to display anyways. Which is true in this condition, why dont you put it in loop to display in order:

for ($index = 0; $index < count($array); $index++) {
    echo $array[$index];
}