Hi. I'm creating a script in which the user could enter a list of medications that he is using. Because it could change from user to user, I'm using a array that "save this information"...This script is fully functional but I will like to change the order of the medications after it is completed manually.

Example: If I have 10 medications...I will like to manually sort the list so the antibiotics are first, then antihypertensive, and all antiglycemic should be together. But so far the list only shows the drugs in the order they were submitted.

There's any easy way to do this?

    Presumably you have some way of identifying which medications are which types. Given that,
    [man]usort[/man] will allow you to reorder arrays any way you like. The comparison function would be something like:

    function compare_medications($a, $b)
    {
        // We array_flip here because we want to look up the category's _position_
        // in the array given its _name_
        $types = array_flip(array('antibiotic', 'hypertensive', 'antiglycemic', ...., 'unknown'));
        $a_type = identify_category($a); // A function to decide what type a given medication is
        $b_type = identify_category($b); // e.g., identify_category('erythromycin') would return 'antibiotic'
        return $types[$b_type] - $types[$a_type];
    }
    
      Write a Reply...