0

I have a list of (xml) items, each item has a category and a name:

Category | Name

1         |  Joe
2         |  Carol
3         |  Bruce
1         |  Michael     
1         |  Alan
2         |  Brian

I want to sort the names ascending within categories descending like so:

Category | Name

3         |  Bruce
2         |  Brian
2         |  Carol
1         |  Alan
1         |  Joe
1         |  Michael

with the aim of creating a Select dropdown on a web page with each category as an OptGroup and the names sorted within the OptGroup. I have very little experience with PHP, I think I need to sort merge arrays, but after many hours trying to understand how to, I'm not making much progress. Any help greatly appreciated

3 Answers3

1
$data[] = array('category' => 1, 'name' => 'Joe');
$data[] = array('category' => 2, 'name' => 'Carol');
$data[] = array('category' => 3, 'name' => 'Bruce');
$data[] = array('category' => 1, 'name' => 'Michael');
$data[] = array('category' => 1, 'name' => 'Alan');
$data[] = array('category' => 2, 'name' => 'Brian');


<?php
// Obtain a list of columns
$category =array();
$name =array();
foreach ($data as $key => $row) {
    $category[$key]  = $row['category'];
    $name[$key] = $row['name'];
}

// Sort the data with category descending, name ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($category, SORT_DESC, $name, SORT_ASC, $data);
echo '<pre>'; print_r($category);
?>
Pankaj katiyar
  • 464
  • 10
  • 26
  • Thanks Pankaj for your time & help. The array_multisort gave this Warning: array_multisort(): Argument #1 is expected to be an array or a sort flag. this is what I have in a foreach loop $expld = explode("_", $title); $data[$cnt]['category'] = $expld[0]; $data[$cnt]['name'] = expld[1]; $cnt++; – user3229980 Oct 01 '14 at 11:43
  • initialize $category =array(); $name =array(); – Pankaj katiyar Oct 01 '14 at 12:12
  • You could at least credit the [documentation example you adapted for this](http://php.net/manual/en/function.array-multisort.php). – Martijn Pieters Sep 21 '17 at 13:01
0

Hi thanks to everybody for your help including all the other Stackoverflow questions which I googled to piece this together, not sure how I did it, bit of a case of an infinite number of monkeys or should that be newbies, anways here's what worked for me. Sorts names ascending within contributions descending

function compare($a, $b) {
    if ($a['contribution'] == $b['contribution']) {
       return ($a['name'] < $b['name']) ? -1 : 1;
    } else {
       return ($b['contribution'] - $a['contribution']);
    }
}    
usort($ads_by_cotribution, 'compare');

It's all magic to me, that's the beauty of it. I didn't include the case of names being equal 'cause there shouldn't be any and as I understand equal values would still stay together just in a different order. I'd like to understand how these functions work, do they continuously walk through the array until there's no change in the order - what we used to call a bubble sort? Does ($b['contribution'] - $a['contribution']) indicate which is the larger? Finley is there a difference between sorting contributions - a numeric field and names - an alpha field?

-1
$name = array("Joe", "Carol", "Bruce", "Michael","Alan","Brian");
sort($name);

php-in built function sort will produce desire result

Kartoch
  • 7,610
  • 9
  • 40
  • 68
Pankaj katiyar
  • 464
  • 10
  • 26