Domanda

Is the following code acceptable for sorting the data within array by commentID in increasing order?

I cannot test the code right now because I have no test values in my database.

$comments = array();

---insert mysql data into $comments here---

$tmp = Array();
foreach($comments as &$ma)
$tmp[] = &$ma["commentID"];
array_multisort($tmp, $comments);

Thanks!

È stato utile?

Soluzione

I would do it the following way:

$comments = array();
// Use your preferred mysql driver here
$result = $mysqli->query($query);
while ($row = $result->fetch_assoc()){
    $comments[$row["id"]] = $row["comment"];
}
$success = ksort($comments);

ksort will sort the array from lowest to highest based on keys. In this instance the key is the id from the database. Let me know if this is unclear, or if this isn't what you want.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top