Jump to content

how to natsort() by key and not by value?


tastro

Recommended Posts

i found a code already on the web. but i don't understand all of it. also if someone could explain to me why is there an & in the function here: knatsort(&$array) before $array ? and why is there an return true; at the end?

 

<?php
function knatsort(&$array){
$array_keys=array_keys($array);
natsort($array_keys);
$new_natsorted_array=array();
$array_keys_2='';
foreach($array_keys as $array_keys_2){
$new_natsorted_array[$array_keys_2]=$array[$array_keys_2];
}
$array=$new_natsorted_array;
return true;
}
knatsort($top_rated_array_with_votes_as_keys);
?>

Link to comment
Share on other sites

You could just use uksort in combination with natsort:

 

uksort($array, 'natsort');

 

Just to explain the use of "&" though, it means pass the array by reference. Normally when you use the sorting functions, you don't assign the return value back, you just call it and the array passed in the parameters is updated. If the "&" wasn't included you would need to return the array within the function and call it like:

 

$returned_array = knatsort($passed_array);

 

You don't need to go to these lengths though. Just use the snippet I provided above.

 

Link to comment
Share on other sites

 

<?php


$array = array('a9' => 0,'a' => 1,'a2' => 2,'b6' => 3,'b1' => 4);
echo '<pre>' . print_r($array, 1) . '</pre>';


$array = array_flip($array);
natsort($array);
$array = array_flip($array);
echo '<pre>' . print_r($array, 1) . '</pre>';
?>

Array ( [a9] => 0 [a] => 1 [a2] => 2 [b6] => 3 [b1] => 4 ) Array ( [a] => 1 [a2] => 2 [a9] => 0 [b1] => 4 [b6] => 3 )

Works but will remove any multiple values ( effectively array_unique )@Tastro[pre]The & indicates that the variable should be "passed by reference", this means that instead of copying the variables value into another variable within the functions scope "passing by value", the function [/size]receives a pointer to the value's memory address ( where it is stored in RAM ) and the value is updated in the scope from which it was called. This means that any changes made to the value within the function scope, take effect on the variable outside of the function.[/pre]

Link to comment
Share on other sites

You could just use uksort in combination with natsort:

 

uksort($array, 'natsort');

 

Just to explain the use of "&" though, it means pass the array by reference. Normally when you use the sorting functions, you don't assign the return value back, you just call it and the array passed in the parameters is updated. If the "&" wasn't included you would need to return the array within the function and call it like:

 

$returned_array = knatsort($passed_array);

 

You don't need to go to these lengths though. Just use the snippet I provided above.

 

thank you very much for your explanation, it's all clear now.

 

i always use this methode:

 

$returned_array = knatsort($passed_array);

 

that's why i didn't know how the & and return true works. :)

 

and thank you for this function:

 

uksort($array, 'natsort');

 

it's really easier to work with such a small piece of code. :) topic solved.

Link to comment
Share on other sites

$array = array(
    3 => 1,
    2 => 2,
    1 => 3
);

uksort($array, 'natsort');
print_r($array);

/* Result:
Array
(
    [1] => 3
    [2] => 2
    [3] => 1
)
*/

 

Works as expected for me. Can you show the way you're using it?

Link to comment
Share on other sites

1. with your code, i get this error: "Warning: natsort() expects exactly 1 parameter, 2 given"

 

2. i tryed it like this:

 

<?php
$array = array(
    22 => 22,
    1 => 1,
    13 => 13
);

uksort($array, 'natsort');
print_r($array);
?>

 

but it doesn't work.

 

i get this:

 

Array ( [13] => 13 [1] => 1 [22] => 22 )

 

but it should be like this:

 

Array ( [1] => 1 [13] => 13 [22] => 22 )

Link to comment
Share on other sites

  • 2 weeks later...

maybe it's because i get an error, this one: "Warning: natsort() expects exactly 1 parameter, 2 given"

 

but i don't know what's wrong and how to fix it. :S

 

this is my code:

 

<?php
$array = array(
    22 => 22,
    1 => 1,
    13 => 13
);

uksort($array,'natsort');
print_r($array);
?>

Link to comment
Share on other sites

lol, i don't know how i missed your post andy, but your code works like a charm! thank you big time!

 

also this one works perfect!

 

<?php
$array=array('22'=>'22','1'=>'1','13'=>'13');
$array=array_flip($array);
natsort($array);
$array=array_flip($array);
print_r($array);
?>

 

 

<?php


$array = array('a9' => 0,'a' => 1,'a2' => 2,'b6' => 3,'b1' => 4);
echo '<pre>' . print_r($array, 1) . '</pre>';


$array = array_flip($array);
natsort($array);
$array = array_flip($array);
echo '<pre>' . print_r($array, 1) . '</pre>';
?>

Array ( [a9] => 0 [a] => 1 [a2] => 2 [b6] => 3 [b1] => 4 ) Array ( [a] => 1 [a2] => 2 [a9] => 0 [b1] => 4 [b6] => 3 )

Works but will remove any multiple values ( effectively array_unique )@Tastro[pre]The & indicates that the variable should be "passed by reference", this means that instead of copying the variables value into another variable within the functions scope "passing by value", the function [/size]receives a pointer to the value's memory address ( where it is stored in RAM ) and the value is updated in the scope from which it was called. This means that any changes made to the value within the function scope, take effect on the variable outside of the function.[/pre]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.