0

I have a simple Array. The goal is, to sort them ascending by the key.

$someUnsortedArray = array("140/142" => "FirstValue", "118/120" => "SecondValue", "122/124" => "ThirdValue", "40/42" => "FourthValue");

ksort($someUnsortedArray);

My Output:

array (size=4)  
  '118/120' => string 'SecondValue' 
  '122/124' => string 'ThirdValue'  
  '140/142' => string 'FirstValue'   
  '40/42' => string 'FourthValue' 

Expected Output:

array (size=4)  
'40/42' => string 'FourthValue'  
'118/120' => string 'SecondValue'   
'122/124' => string 'ThirdValue'  
'140/142' => string 'FirstValue' 

What's the function in php I am searching for?

0

6 Answers 6

6

You could use uksort() in this case:

$someUnsortedArray = array("140/142" => "FirstValue", "118/120" => "SecondValue", "122/124" => "ThirdValue", "40/42" => "FourthValue");

uksort($someUnsortedArray, function($a, $b){
    $a = str_replace('/', '', $a);
    $b = str_replace('/', '', $b);
    return $a - $b;
});

echo '<pre>';
print_r($someUnsortedArray);
Sign up to request clarification or add additional context in comments.

5 Comments

In the theory it would work.. But is there any solution without replacing the "/" without nothing?
@Tyralcori you need to sort it in custom since you won't get the desired values if they are going to be compared as strings
@Tyralcori if you do not want a replace you could explode it
At least, that's my solution. Thank you very much.
@Tyralcori glad this helped by the way, barry's answer is much good, you could also pick that one
2

As an alternative, you can also make use of the natural order string compare function to compare the keys

function sortKey($a, $b) {
    return strnatcmp($a, $b);
}

uksort($someUnsortedArray,"sortKey");

Comments

2

Checking php manual:

http://php.net/manual/en/function.ksort.php

Use ksort() with SORT_NUMERIC flag.

$someUnsortedArray = array("140/142" => "FirstValue", "118/120" => "SecondValue", "122/124" => "ThirdValue", "40/42" => "FourthValue");
ksort($someUnsortedArray, SORT_NUMERIC);

echo '<pre>';
print_r($someUnsortedArray);

Sample Output

4 Comments

what about SORT_NUMERIC
"40/42" or "118/120" aren't numeric values, they are string.
My bad, it works indeed. If you edit your answer I'll cancel my downvote. (Can't now)
Sidenote: As you can see this answer has a negative score, but don't downvote this answer anymore as this is already correct. +1
0

Use the function uksort and pass the function a custom function/method that will do the right conversion based on your needs.

Here is the PHP manual page for he function uksort http://php.net/manual/en/function.uksort.php

Comments

0

The result is the expected behaviour, because if you sort Strings in alphanumerical order 1* is always before 4*.

If you want to have it sorted by number you'll have to split your string, convert the elements to number and sort them by number.

Implementation depends on what you want to achieve. Just a list of the Keys? Then you could iterate in an foreach loop adressing the keys for example.

There won't be an "out-of-the-box" PHP Function.

Comments

0
$someUnsortedArray = array("5/142" => "FirstValue", "118/120" => "SecondValue", "122/124" => "ThirdValue", "40/42" => "FourthValue");

uksort($someUnsortedArray, function($a, $b) {
    // to avoid manipulating them as a string ....      
    return ((float)$a)-((float)$b);
});
var_dump($someUnsortedArray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.