Reply to Re: array multisort question

Your name:

Reply:


Posted by Rik on 08/23/06 10:25

annmartinson@gmail.com wrote:
> annmartinson@gmail.com wrote:
>> yes, I've read and tried umpteen of the sort functions and still
>> can't get it right. which one do I need???
>>
>> /////////////////////////////////////////////
>> here is my array:
>>
>> $final[0]['name'] = "fire";
>> $final[0]['value'] = 0;
>> $final[0]['id'] = 1;
>> $final[0]['name'] = "air";
>> $final[0]['value'] = 0;
>> $final[0]['id'] = 2;
>>
>> Within my functional code, the 'value' keys are changed dynamically
>> based on returns from a form on a website. At this point, I want to
>> rearrange the array SORT_NUMERICAL, SORT_DESC so that the first two
>> records are the highest value. I then assign these two 'id' keys to
>> separate variables and use them to post the info requested back to
>> the site visitor.
>>
>> I've tried
>> --- (1)
>>
>> $final2 = arsort($final[value], SORT_NUMERIC);
>>
>> $id = $final2[0]['id'];
>> $id2 = $final2[1]['id'];
>>
>> ---(2)
>> $final = array_multisort($final[value], SORT_NUMERIC, SORT_DESC);
>>
>> $id = $final[0]['id'];
>> $id2 = $final[1]['id'];
>>
>> ---(3)
>> $final2 = array_multisort($final[value], SORT_NUMERIC, SORT_DESC,
>> $final[key]);
>>
>> $id = $final2[0]['id'];
>> $id2 = $final2[1]['id'];
>>
>>
>> every one of these gave me various errors.
>>
>> help is much appreciated. I'm sure the answer is MUCH simpler than
>> I'm managing :)

<?php

$final[0]['name'] = "fire";
$final[0]['value'] = 0;
$final[0]['id'] = 1;
$final[1]['name'] = "air";
$final[1]['value'] = 2;
$final[1]['id'] = 2;
$final[2]['name'] = "infinity";
$final[2]['value'] = 6;
$final[2]['id'] = 3;
$final[3]['name'] = "water";
$final[3]['value'] = 1;
$final[3]['id'] = 4;
$final[4]['name'] = "earth";
$final[4]['value'] = 10;
$final[4]['id'] = 5;

function cmp($a, $b){
if($a['value']==$b['value']) return 0;
return ($a['value']>$b['value']) ? -1 : 1;
}
usort($final,'cmp');
print_r($final);
?>
--
Grtz,

Rik Wasmus

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация