2010-08-11 7 views
3

J'ai un tableau de nombres, par exemple, myarray = (45,3,56,7,21). Ce que je dois être en mesure de faire est de classer les valeurs dans un autre tableau donc pour ce qui précède, je finirais par myarray2 = (4,1,5,2,3).Comment affecter un numéro de rang à un tableau

Un grand merci,

Adam

+0

Quelqu'un peut-il vous conseiller comment résoudre ce problème, mais classer le tableau dans l'ordre décroissant? – ajcw

Répondre

2

Voilà, solution complète:

<?php 

$myarray = array(45,3,56,7,21); 

//create a copy and sort 
$myarray_copy = $myarray; 
sort($myarray_copy); 

//reverses key and values 
$myarray_copy = array_flip($myarray_copy); 

//create result by using keys from sorted values + 1 
foreach($myarray as $val) 
    $myarray2[] = $myarray_copy[$val]+1; 

//print final array 
print_r($myarray2); 

/* 
Output: 

Array ([0] => 4 [1] => 1 [2] => 5 [3] => 2 [4] => 3) 
*/ 
?> 
0

Prenez un certain nombre de votre tableau, comptez combien de nombres sont plus petits que celui-là, ajouter un et vous avez le rang.

1

Très intéressant.

Ce que je peux venir avec sur place est quelque chose comme ceci:

Trier le tableau et mettez dans un autre tableau, puis remplissez MyArray2 en recherchant le tableau intermédiaire pour la valeur, puis remplir des années MyArray2 valeurs avec l'index.

$myarraytemp = sort clone $myarray; 

foreach ($myarray as $num) 
{ 
    array_push($myarray2, array_search($num,$myarraytemp)); 
} 

Sinon, vous pouvez le faire:

foreach ($myarray as $num) 
{ 
    $rank=0; 
    foreach ($myarray as $innernum) 
    { 
     if($innernum<= $num) 
      $rank++;    
    } 
    array_push($myarray2, $rank); 
} 
+0

Si vous remarquez des erreurs syntaxiques minuscules ici s'il vous plaît laissez-moi savoir, fait beaucoup de perl ces derniers temps, de sorte que la syntaxe perl pourrait accidentellement saigner dans mon php. –

0

est ici une fonction pour le faire:

function get_array_ranking($array){ 
    $ranking = array(); 
    foreach ($array as $number) { 
    $smaller = 1; 
    foreach ($array as $number2) { 
     if ($number2 < $number) $smaller++; 
    } 
    $ranking[] = $smaller; 
    } 
    return $ranking; 
} 
1

Eh bien, vous pourriez faire quelque chose comme:

$b = $a = array(45,3,56,7,21); 
sort($b); 
$r = array_map(
    function ($number) use ($b) { 
     return array_search($number, $b) + 1; 
    }, $a); 

Mais pour le rendre efficace, vous devez mettre en œuvre votre o wn fonction de tri. Voici une implémentation Java je ne pouvais pas la peine de traduire (de weka):

/** 
* Sorts a given array of integers in ascending order and returns an 
* array of integers with the positions of the elements of the original 
* array in the sorted array. The sort is stable. (Equal elements remain 
* in their original order.) 
* 
* @param array this array is not changed by the method! 
* @return an array of integers with the positions in the sorted 
* array. 
*/ 
public static int[] sort(int[] array) { 

    int[] index = new int[array.length]; 
    int[] newIndex = new int[array.length]; 
    int[] helpIndex; 
    int numEqual; 

    for (int i = 0; i < index.length; i++) { 
     index[i] = i; 
    } 
    quickSort(array, index, 0, array.length - 1); 

    // Make sort stable 
    int i = 0; 
    while (i < index.length) { 
     numEqual = 1; 
     for (int j = i + 1; ((j < index.length) 
       && (array[index[i]] == array[index[j]])); 
       j++) { 
      numEqual++; 
     } 
     if (numEqual > 1) { 
      helpIndex = new int[numEqual]; 
      for (int j = 0; j < numEqual; j++) { 
       helpIndex[j] = i + j; 
      } 
      quickSort(index, helpIndex, 0, numEqual - 1); 
      for (int j = 0; j < numEqual; j++) { 
       newIndex[i + j] = index[helpIndex[j]]; 
      } 
      i += numEqual; 
     } else { 
      newIndex[i] = index[i]; 
      i++; 
     } 
    } 
    return newIndex; 
} 

private static void quickSort(int[] array, int[] index, 
     int left, int right) { 

    if (left < right) { 
     int middle = partition(array, index, left, right); 
     quickSort(array, index, left, middle); 
     quickSort(array, index, middle + 1, right); 
    } 
} 
0

assez simple si vous utilisez une fonction de tri qui maintient les clés originales comme asort:

En outre, étant donné que celui-ci utilise l'interne tri php, le temps d'exécution pour le tri est n log (n) Je crois

$myArray = (45,3,56,7,21); 
$myArrayCopy = $myArray; 
//asort will sort an array while preserving the key value 
asort($myArrayCopy); 

foreach($myArraycopy as $key => $value) 
{ 
    //map the keys of the array to the original index locations 
    $sortIndexArray[] = $key; 
} 
Questions connexes