2014-03-02 2 views
-4

목록을 높은 값에서 낮은 값으로 정렬하고 싶습니다. 그 산출물의 가치는 여론 조사에서 나온 것이다. optionOne : 11 : optionTwo : 5 :PHP로 목록을 값으로 정렬하는 방법

내가 일을 시도

function votingScore($item, $itemvoted) { 
$hector=count($itemvoted);$totalvotes=0;$in=0;$stepstr=''; 
$totalvotes=SumArray($itemvoted); 
$in=0; 
if ($totalvotes==0) { $totalvotes=0.0001; } 
while ($in<$hector) { 
    $stepstr=$stepstr.'<li>'.stripslashes($item[$in]).(int)(($itemvoted[$in]/$totalvotes)*100).'% '; 
    $stepstr=$stepstr.'</li>'; 
    $in++; 
} 
return '<ul>'.$stepstr.'</ul>'; } 

투표는 다음과 같이 lokes 텍스트 파일에 저장됩니다 : 여기

목록을 생성하는 코드입니다 다음

arsort($stepstr); 
foreach ($stepstr as $key => $val) { 
echo "stepstr[" . $key . "] = " . $val . "\n"; } 

FIX :

내가 제대로 코드를 이해한다면
function votingScore($item, $itemvoted) { 
    $combineSort = array_multisort($itemvoted, SORT_DESC, $item, SORT_DESC); //added this line and got it to work 
    $hector=count($itemvoted);$totalvotes=0;$in=0;$stepstr=''; 
+6

http://www.php.net/manual/array.sorting.php –

+0

@ jan1337z이 (가) 어서 오세요! 나는이 기능이 몇 가지 중요한 것을 보여줄 책임이 있다고 생각합니다 ... 물건. 물건은 뭐든간에. 분명히 중요한 것입니다. 그것은 가능해야합니다. 아마? 나는 모른다. – matewka

+1

@ jan1337z 더 좋아하십니까? – florisvl

답변

0

방금 ​​같은 $itemvotedarsort를 호출 할 필요가 : 어떻게 생겼는지 $itemvoted에 따라

function votingScore($item, $itemvoted) { 
    $success = arsort($itemvoted); //Success will either be True or False 
    .... 

그러나 당신은 다른 정렬을 할 수 있습니다 (이 연관인지) @George_Cummins에 의해 명시된대로 작동합니다. link :

+0

감사합니다. @ajon! 이것은 일의 일부분을합니다. 이상한 것은 목록에있는 숫자 만 정렬하고 값에 속하는 옵션은 정렬하지 않는다는 것입니다. 값이 같다 : optionOne : 12 optionTwo : 18 optionThree : 13 가 다음과 같이 정렬됩니다 : optionOne : 18 optionTwo : 13 optionThree : 12 하지만 내가 그것을처럼되고 싶어요 : optionTwo : 18 optionThree : 13 optionOne : 12 – florisvl

+0

ok ... 편집을 참조하십시오. 지시어를 동일하게 유지하면서 배열을 역순으로 정렬하는 arsort를 사용해야합니다. $ itemvoted는 값을 옵션에 매핑하는 관련 배열입니다 (예 : {[12] => optionOne, [18] => optionTwo, ...} – ajon

+0

알아 냈어! 나는 다음을 사용했다 : \t $ combineSort = array_multisort ($ itemvoted, SORT_DESC, $ item, SORT_DESC); – florisvl

관련 문제