Steven Trainor
Steven Trainor

Reputation: 1255

Displaying results in descending order of value within array variable

Hi I was wondering if anyone could help me. I have created a questionnaire, there are multiple results (displayed as a %) outputted from the questionnaire. I am able to get these results displayed on a results.php page, along with the corresponding name ($careername) and corresponding link ($link). E.g. I have 5 results which each link to a corresponding career and link and these are displayed on the results.php page. However I need the result, careername and link to display in descending order of the result value. As of now they are displaying in random order. Below is the code I am working with, if anyone has any ideas I would be grateful.

<?php
$careername1 = 'Nursing '; 
$careername2 = 'Footballer ';
$careername3 = 'Dentist ';
$careername4 = 'Hairdressing ';
$careername5 = 'IT ';
?>

<?php
$link1 = '<br><a href="http://www.nhscareers.nhs.uk/explore-by-career/nursing/" target="_blank">More information on Nursing</a></br></br>';
$link2 = '<br><a href="#" target="_blank">More information on Footballing</a></br>  </br>';
$link3 = '<br><a href="#" target="_blank">More information on Dentistry</a></br></br>';
$link4 = '<br><a href="#" target="_blank">More information on Hairdressing</a></br></br>';
$link5 = '<br><a href="#" target="_blank">More information on IT</a></br></br>';
?>
<?php
$nursing = array($careername1, $result1, "% ", $link1);
$footballer = array($careername2, $result2, "% ", $link2);
$dentist = array($careername3, $result3, "% ", $link3);
$hairdresser = array($careername4, $result4, "% ", $link4);
$IT = array($careername5, $result5, "% ", $link5);
?>
<h1>Your results are listed below:</h1>

<?php
$items = array("$nursing", "$footballer", "$dentist", "$hairdresser", "$IT");
arsort($items);
foreach ($items as $key => $val) {
echo "$key = $val\n";
}
?>

Upvotes: 0

Views: 2356

Answers (2)

RafH
RafH

Reputation: 4544

// some test values
$nursing = array("nursing", 5, "% ", "");
$footballer = array("footballer", 15, "% ","");
$dentist = array("dentist", 25, "% ", "");
$hairdresser = array("hairdresser", 0, "% ", "");
$IT = array("IT", 50, "% ", "");

$items = array($nursing, $footballer, $dentist, $hairdresser, $IT);

foreach ($items as $item) {
 echo $item[1].'->'.$item[0].'<br>';;
}

output unsorted

5->nursing
15->footballer
25->dentist
0->hairdresser
50->IT

sort on index 1 descending

function compare($a, $b) {
    if ($a[1] == $b[1]) {
        return 0;
    }
    return ($a[1] < $b[1]) ? -1 : 1;
}

usort($items, 'compare');

foreach ($items as $item) {
  echo $item[1].'->'.$item[0].'<br>';;
}

output sorted

0->hairdresser
5->nursing
15->footballer
25->dentist
50->IT

http://www.php.net/manual/fr/function.usort.php

Upvotes: 1

user975343
user975343

Reputation:

$items = array("$nursing", "$footballer", "$dentist", "$hairdresser", "$IT");

Notice that "$nursing" should be $nursing , etc.

$items = array($nursing, $footballer,$dentist, $hairdresser,$IT);

If that doesn't work, you may need to write a compare function of your own and use it as the second argument of the arsort function.

For further information , read http://php.net/manual/en/function.arsort.php

(*Writing a comparing function is not as difficult as it might seems like)

Upvotes: 0

Related Questions