0

I have an array which is inserted into an HTML Table

enter image description here

The problem is I want to sort it according to the value of counter. I tried an example which looks like this but it won't work.

foreach($analysis_data as $nr => $inhalt){
    $country[$nr] = strtolower($inhalt['country']);
    $counter[$nr] = strtolower($inhalt['counter']);
}

array_multisort($counter, SORT_ASC, $analysis_data);,

Does anybody know how to sort this multi dimensional array?

  • `SORCT_ASC`? Maybe `SORT`? – u_mulder Jul 21 '20 at 09:53
  • Yes there was a typing mistake but didn't changed the result –  Jul 21 '20 at 10:16
  • @fhprogrammer there was a similar problem posted https://stackoverflow.com/questions/1597736/how-to-sort-an-array-of-associative-arrays-by-value-of-a-given-key-in-php maybe it can help you – meewog Jul 21 '20 at 10:55
  • @MehrdadDastgir: Yes just saw it that this problem was similar. Was able to fix it with help of this –  Jul 21 '20 at 11:21

2 Answers2

0

Due to the documentation third parameter of array_multisort function is array1_sort_flags. But you pass array with data.

I assume this is the right way to use it in your case.

array_multisort($counter, SORT_ASC);
Den Kison
  • 1,074
  • 2
  • 13
  • 28
0
$analysis_data  = array(
    array('counter'=>34,'country'=>'Germany'),
    array('counter'=>2,'country'=>'Vorarlberg'),
    array('counter'=>9,'country'=>'Oberoesterreich'),
    array('counter'=>5,'country'=>'Wien'),
    array('counter'=>3,'country'=>'Switzerland'),
    array('counter'=>6,'country'=>'Salzburg'),
    array('counter'=>1,'country'=>'Niederoesterreich'),
    array('counter'=>3,'country'=>'Czech Republic'),
    array('counter'=>1,'country'=>'Steiermark'),
);

foreach($analysis_data as $nr => $inhalt){
    $country[$nr] = strtolower($inhalt['country']);
    $counter[$nr] = strtolower($inhalt['counter']);
}

array_multisort($counter, SORT_ASC, $analysis_data);

enter image description here

xNoJustice
  • 516
  • 5
  • 8
  • Thanks but I want it ascending not descending. Keeping that out of mind, it doesn't work like this either –  Jul 21 '20 at 11:10