Ask a Question related to PHP Development, Design and Development.
-
Boefje #1
sort array by key
Hi,
What I want is simple, but I can't figure it out at the moment.
Let's say this is an array names $matches:
Array
(
[0] => Array
(
[teamid] => 137
[teamnaam] => Weet ik Veel ??
[speeldagid] => vr
[speeldagvolgnr] => 5
)
[1] => Array
(
[teamid] => 27
[teamnaam] => Tycoon
[speeldagid] => di
[speeldagvolgnr] => 2
)
[2] => Array
(
[teamid] => 36
[teamnaam] => Arabier 1
[speeldagid] => do
[speeldagvolgnr] => 4
)
[3] => Array
(
[teamid] => 105
[teamnaam] => Road Runners
[speeldagid] => do
[speeldagvolgnr] => 4
)
)
And I want to sort it on key 'speeldagvolgnr', how do I do this?
So I want the array back
Array
(
[0] => Array
(
[teamid] => 27
[teamnaam] => Tycoon
[speeldagid] => di
[speeldagvolgnr] => 2
)
[1] => Array
(
[teamid] => 36
[teamnaam] => Arabier 1
[speeldagid] => do
[speeldagvolgnr] => 4
)
[2] => Array
(
[teamid] => 105
[teamnaam] => Road Runners
[speeldagid] => do
[speeldagvolgnr] => 4
)
[3] => Array
(
[teamid] => 137
[teamnaam] => Weet ik Veel ??
[speeldagid] => vr
[speeldagvolgnr] => 5
)
)
I have looked at the sort functions at PHP.net, but could not find the
solution.
Can you help me out? Thanx.
Boefje Guest
-
Sort Array in datagrid
How do you sort an array in your datagrid? I always want to be able to sort my array by an instance number. If you add a new instance I want it to... -
Sort Array
Hi Can anyone give me a pointer on sorting this array below #curr.getRateCurrencyCode()# (lowest to highest values) <cfloop from ='1' to... -
Sort a 2D array
Hello, I have a 2D array which I would like to sort. I take a simple example: $tab = 'toto';$tab = 'toto'; $tab = 'aaaa';$tab = 'titi'; $tab... -
sort w/o using an array
I am trying to figure out if there is a way to do a sort that doesn't involve putting an entire file in memory. This kind of thing is available in... -
Help me sort a two - d array
I need help sorting a multidimensional array. I have an array myarray(column_no, row_no) it has 5 columns for this example I have used 4 rows... -
Janwillem Borleffs #2
Re: sort array by key
Boefje wrote:
You have missed usort():> I have looked at the sort functions at PHP.net, but could not find the
> solution.
>
function cmp($a, $b) {
if ($a['speeldagvolgnr'] == $b['speeldagvolgnr']) {
return 0;
}
return ($a['speeldagvolgnr'] < $b['speeldagvolgnr']) ? -1 : 1;
}
usort($matches, "cmp");
JW
Janwillem Borleffs Guest
-
Janwillem Borleffs #3
Re: sort array by key
Boefje wrote:
Don't multipost (same question asked in alt.comp.lang.php in a seperate
thread)
Did you test your example? Then you will have realised that your multi_sort> Indeed. Thanks.
>
> An example...
>
function doesn't work. The following will:
function multi_sort(&$array, $key) {
usort($array,
create_function(
'$a,$b',
'if ($a["'.$key.'"] == $b["'.$key.'"]) return 0;' .
'return ($a["'.$key.'"] < $b["'.$key.'"]) ? -1 : 1;'
)
);
}
JW
Janwillem Borleffs Guest
-
fanonimus #4
Re: sort array by key
Janwillem Borleffs wrote:> *Boefje wrote:
>
> Don't multipost (same question asked in alt.comp.lang.php in a
> seperate
> thread)
>>> > Indeed. Thanks.
> >
> > An example...
> >
> Did you test your example? Then you will have realised that your
> multi_sort
> function doesn't work. The following will:
>
> function multi_sort(&$array, $key) {
> usort($array,
> create_function(
> '$a,$b',
> 'if ($a["'.$key.'"] == $b["'.$key.'"]) return 0;' .
> 'return ($a["'.$key.'"] < $b["'.$key.'"]) ? -1 : 1;'
> )
> );
> }
>
>
> JW *
Hi!
Why the below example doesn't work?
function cmp ($a, $b) {
if ($a["speeldagvolgnr"] == $b["speeldagvolgnr"]) {
return 0;
}
return ($a["speeldagvolgnr"] < $b["speeldagvolgnr"]) ? -1 : 1;
}
uksort($tab,"cmp");
and the next example works very fine?
function multi_sort(&$array, $key) {
usort($array,
create_function(
'$a,$b',
'if ($a["'.$key.'"] == $b["'.$key.'"]) return 0;' .
'return ($a["'.$key.'"] < $b["'.$key.'"]) ? -1 : 1;'
)
);
}
multi_sort( $tab,"speeldagvolgnr");
Thanks very much for an explanation.
F
--
fanonimus
------------------------------------------------------------------------
Posted via [url]http://www.codecomments.com[/url]
------------------------------------------------------------------------
fanonimus Guest
-
fanonimus #5
Re: sort array by key
fanonimus wrote:Thanks> *Hi!
> Why the below example doesn't work?
>
> function cmp ($a, $b) {
> if ($a["speeldagvolgnr"] == $b["speeldagvolgnr"]) {
> return 0;
> }
> return ($a["speeldagvolgnr"] < $b["speeldagvolgnr"]) ? -1 : 1;
> }
>
> uksort($tab,"cmp");
>
> and the next example works very fine?
>
> function multi_sort(&$array, $key) {
> usort($array,
> create_function(
> '$a,$b',
> 'if ($a["'.$key.'"] == $b["'.$key.'"]) return 0;' .
> 'return ($a["'.$key.'"] < $b["'.$key.'"]) ? -1 : 1;'
> )
> );
> }
>
> multi_sort( $tab,"speeldagvolgnr");
>
>
> Thanks very much for an explanation.
> F *
--
fanonimus
------------------------------------------------------------------------
Posted via [url]http://www.codecomments.com[/url]
------------------------------------------------------------------------
fanonimus Guest



Reply With Quote

