0

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?

1
  • Is there a performance issue? Or there's some reason you prefers other solution than ksort? Commented Jul 10, 2013 at 1:50

2 Answers 2

1

i think, u need use ksort before displaying

Sign up to request clarification or add additional context in comments.

Comments

0

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];
}

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.