Php array sort numbers leading zeroes numeric string

Hello, I have a curios problem sorting an array with numbers with leading zeroes as either numeric or string.
Here is my array:

$target[] = array('06', '14', '24', '35', '37', '49');
$target[] = array('06', '23', '37', '44', '48', '49');
$target[] = array('06', '10', '14', '22', '44', '49');
$target[] = array('01', '06', '14', '36', '37', '44');
$target[] = array('14', '18', '19', '29', '37', '49');
$target[] = array('18', '06', '09', '44', '14', '30');

I tried using methods SORT_NUMERIC and SORT_STRING as follows:

sort($target, SORT_NUMERIC);

and in the last array, I see this:

  5 => 
    array (size=6)
      0 => string '18' (length=2)
      1 => string '06' (length=2)
      2 => string '09' (length=2)
      3 => string '44' (length=2)
      4 => string '14' (length=2)
      5 => string '30' (length=2)

I tried manually editing to correct the array but still see the same result.
I also tried to cast the values to integer as follows but again still see the same result:

//	Cast all values in array as integer type values otherwise leave as strings
$passmydraws === array_merge(array($target[0]),array_map('intval', array_slice($target, 0, 5))); // strval  intval
echo "Cast all values in passmydraws <pre>"; var_dump($target); echo "</pre>"; echo "<br>\n";

What am I missing or how can I remove the leading zeroes from the entire array if that will help?

What result are you trying to produce from the posted data, because sort() sorts the elements of a single array, not an array of arrays.

I forgot to mention that I am using Php 8.1.
This is the output of the sorted array using print_r:

Array
(
    [0] => Array
        (
            [0] => 01
            [1] => 06
            [2] => 14
            [3] => 36
            [4] => 37
            [5] => 44
        )

    [1] => Array
        (
            [0] => 06
            [1] => 10
            [2] => 14
            [3] => 22
            [4] => 44
            [5] => 49
        )

    [2] => Array
        (
            [0] => 06
            [1] => 14
            [2] => 24
            [3] => 35
            [4] => 37
            [5] => 49
        )

    [3] => Array
        (
            [0] => 06
            [1] => 23
            [2] => 37
            [3] => 44
            [4] => 48
            [5] => 49
        )

    [4] => Array
        (
            [0] => 14
            [1] => 18
            [2] => 19
            [3] => 29
            [4] => 37
            [5] => 49
        )

    [5] => Array
        (
            [0] => 18
            [1] => 06
            [2] => 09
            [3] => 44
            [4] => 14
            [5] => 30
        )

)

You should show us your code. What you are doing in the two lines of code in your first post is senseless. You slice the array to get only the first 5 arrays out of your 6 and then map the array of arrays and then merge them. Completly confusing. The result you are showing has nothing to do with the source arrays. The array contains totally different numbers.

That’s it, I was slicing only 5 of the 6 arrays and I didn’t see it.
Thank you.

Slicing 6 out of 6 is not really senseful isn’t it?

PHP’s sort() function can be used to sort an array of numeric strings with leading zeroes numerically, maintaining numeric order. The function takes into account the numeric value of each string, ignoring leading zeroes. The output will be a sorted array, with the leading zeroes preserved. To maintain leading zeroes and ensure correct numeric sorting, no special steps are needed, as SORT_NUMERIC handles numeric interpretation correctly.

What about using natsort()?

https://www.php.net/manual/en/function.natsort.php

notably, natsort maintains key identifiers. You’d also need to apply it to each member of the array (because your array is actually a multidimensional array of size 6x6).

Here’s what you might check;

$target = [
    ['06', '14', '24', '35', '37', '49'],
    ['06', '23', '37', '44', '48', '49'],
    ['06', '10', '14', '22', '44', '49'],
    ['01', '06', '14', '36', '37', '44'],
    ['14', '18', '19', '29', '37', '49'],
    ['18', '06', '09', '44', '14', '30']
];

function sortArray(&$arr) {
    foreach ($arr as &$subArray) {
        $intArray = array_map('intval', $subArray);
        sort($intArray, SORT_NUMERIC);
        $subArray = array_map(function($num) {
            return str_pad($num, 2, '0', STR_PAD_LEFT);
        }, $intArray);
    }
}

sortArray($target);

usort($target, function($a, $b) {
    return strcmp(implode('', $a), implode('', $b));
});

echo "<pre>";
print_r($target);
echo "</pre>";

1. Convert strings to integers, sort, then convert back to strings with leading zeroes.
2. Sort the main array if needed.

That function works well. Thanks

#OneLiner
array_map(function($x) { natsort($x); return array_values($x); },$target);

(or to do it in-place without return, array_walk instead of map)

1 Like