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;
$final[0]['name'] = "infinity";
$final[0]['value'] = 0;
$final[0]['id'] = 3;
$final[0]['name'] = "water";
$final[0]['value'] = 0;
$final[0]['id'] = 4;
$final[0]['name'] = "earth";
$final[0]['value'] = 0;
$final[0]['id'] = 5;
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 :) 4 4174
SORRY, here is the correct array ... that's not the problem.
$final[0]['name'] = "fire";
$final[0]['value'] = 0;
$final[0]['id'] = 1;
$final[1]['name'] = "air";
$final[1]['value'] = 0;
$final[1]['id'] = 2;
$final[2]['name'] = "infinity";
$final[2]['value'] = 0;
$final[2]['id'] = 3;
$final[3]['name'] = "water";
$final[3]['value'] = 0;
$final[3]['id'] = 4;
$final[4]['name'] = "earth";
$final[4]['value'] = 0;
$final[4]['id'] = 5;
---------------------------------------------------------------- an**********@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;
$final[0]['name'] = "infinity";
$final[0]['value'] = 0;
$final[0]['id'] = 3;
$final[0]['name'] = "water";
$final[0]['value'] = 0;
$final[0]['id'] = 4;
$final[0]['name'] = "earth";
$final[0]['value'] = 0;
$final[0]['id'] = 5;
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 :)
an**********@gmail.com wrote:
an**********@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
No idea why this works, but I do appreciate it very much. plugged it
in, works like a charm. thanks!! an**********@gmail.com wrote:
No idea why this works, but I do appreciate it very much. plugged it
in, works like a charm. thanks!!
Well for future reference, you may need another sort, so a short
explanation:
usort($array,'function_name');
function_name is the custom name of a function to sort by. This function
will get 2 variables to compare, $a and $b, which are 2 values from your
array, so in this case:
$a = array('id'=>1,'name'=>'fire,'value'=>0);
Likewise for b.
This function will have to return either:
0 when sorting should return equals.
-1 when $a should go before $b
1 when $a should come after $b
Now we only want to sort on the 'value' key, so we in the function we only
compare the 'value' key of 'a' and 'b'. In normal sorting, the whole array
$a and $b are compared.
Grtz,
--
Rik Wasmus This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: buda |
last post by:
Hi,
I've been wondering for a while now (and always forgot to ask :) what is the
exact quote from the Standard that forbids the use of (&array)
(when x >= number_of_columns) as stated in the FAQ...
|
by: Geoff Cox |
last post by:
Hello,
I am trying to get a grip on where to place the initialization of two
arrays in the code below which was created using Visual C++ 2005
Express Beta 2...
private: static array<String^>^...
|
by: anonymous |
last post by:
I have couple of questions related to array addresses. As they belong
to the same block, I
am putting them here in one single post. I hope nobody minds:
char array;
int address;
Questions...
|
by: Leszek |
last post by:
Hi.
Is it possible in javascript to operate on an array without knowing how mamy
elements it has?
What i want to do is sending an array to a script, and this script should
add all values from...
|
by: Balaskas Evaggelos |
last post by:
Hi,
does anyone know how i can sort a multi-dimensional array
by a specific field ?
for example i want to sort arr where n=2,
but i need the data of every array to follow that order.
...
|
by: heddy |
last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int
Newsize); and the newsize is smaller then what the array was
previously, are the resources allocated to the objects that are...
|
by: Andrew Poulos |
last post by:
Say I have two arrays with the same number of elements that are related:
var a1 = ;
var b2 = ;
I need to sort a1 but have the order of the elements in b2 reflect the
any new order in a1. So...
|
by: millw0rm |
last post by:
i got 2 array one holds the actual data n another is user generated
sort order... how to sort array #1 according to array #2???? array
shld be sorted according to sortorder
Array #1
(
=Array...
|
by: mab464 |
last post by:
I have this code on my WAMP server running on my XP machine
if ( isset( $_POST ) )
{
for($i=0; $i<count($_POST);$i++)
{
if ($ans != NULL )
$ans .= ", " . $_POST ; // Not the first...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |