La soluzione proposta precedente sembra logico, ma semplicemente doens't lavoro:
ksort($arrTags);
arsort($arrTags);
Il codice PHP completo per realizzare il richiesto Ordinamento, sarà:
$k = array_keys($arrTags);
$v = array_values($arrTags);
array_multisort($k, SORT_ASC, $v, SORT_DESC);
$arrTags = array_combine($k, $v);
prega di notare che array_multisort() utilizza riferimenti su input dell'utente, quindi dovrete utilizzare due variabels temporanee ($ k e $ v) per la fornitura di contenuti come input da parte dell'utente. In questo modo array_multisort() può modificare il contenuto. Successivamente, ricostruire l'array ordinato tramite array_combine().
Ho costruito una funzione riutilizzabili per eseguire questa operazione:
<?php
/**
* Sort a multi-dimensional array by key, then by value.
*
* @param array Array to be sorted
* @param int One of the available sort options: SORT_ASC, SORT_DESC, SORT_REGULAR, SORT_NUMERIC, SORT_STRING
* @param int One of the available sort options: SORT_ASC, SORT_DESC, SORT_REGULAR, SORT_NUMERIC, SORT_STRING
* @return void
* @example The following array will be reordered:
* $a = array(
* 'd' => 4,
* 'c' => 2,
* 'a' => 3,
* 'b' => 1,
* 'e' => 2,
* 'g' => 2,
* 'f' => 2,
* );
* SortArrayByKeyThanValue($a); # reorder array to: array(
* 'b' => 1,
* 'c' => 2,
* 'e' => 2,
* 'f' => 2,
* 'g' => 2,
* 'a' => 3,
* 'd' => 4,
* );
* @author Sijmen Ruwhof <sijmen(a)secundity.com>
* @copyright 2011, Secundity
*/
function SortArrayByKeyThanValue (&$pArray, $pSortMethodForKey = SORT_ASC, $pSortMethodForValue = SORT_ASC)
{
# check user input: sorting is not necessary
if (count($pArray) < 2)
return;
# define $k and $v as array_multisort() needs real variables, as user input is put by reference
$k = array_keys ($pArray);
$v = array_values($pArray);
array_multisort(
$v, $pSortMethodForValue,
$k, $pSortMethodForKey
);
$pArray = array_combine($k, $v);
}
?>
@MMior Quindi qual è la soluzione non complicata? –
vedere @Jon Bernhardt qui sotto per un buon esempio di implementazione. –