0

For Example, my Array is something like this :

$testArray = [
    "01-01A" => [
        'name' => "test1",
        'test' => "01-01A"
    ],
    "01-06B" => [
        'name' => "test2",
        'test' => "01-06B"
    ],
    "02-05A" => [
        'name' => "test3",
        'test' => "01-05A"
    ],
    "03-33B" => [
        'name' => "test4",
        'test' => "01-33B"
    ],
    "01-044B" => [
        'name' => "test5",
        'test' => "01-044B"
    ],
    "07-098A" => [
        'name' => "test1",
        'test' => "01-098A"
    ],
    "09-0963B" => [
        'name' => "test2",
        'test' => "01-0963B"
    ],
    "10-0596C" => [
        'name' => "test3",
        'test' => "01-0596C"
    ],
    "99-0586C" => [
        'name' => "test4",
        'test' => "01-0586C"
    ],
    "21-984E" => [
        'name' => "test5",
        'test' => "01-984E"
    ]
];

How can I sort this array using a key or 'test' ??

Output:

01-01A
01-05A
01-098A
01-06B
01-33B
01-044B
01-0963B
01-0586C
01-0596C
01-984E

3 Answers 3

0

According to my knowledge,This will not sort because key "test" value is a combination of numeric, string and dash.

If you change key "test" value without dash then natsort() function will sort the array having value(numeric with string).

0

Solution :

uksort($testArray, function ($key1, $key2) {
    // Extract letters from keys
    $letter1 = substr($key1, -1);
    $letter2 = substr($key2, -1);
    // Compare letters
    $letterComparison = strcmp($letter1, $letter2);
    // If letters are the same, sort by numeric part of keys
    if ($letterComparison === 0) {
        $numericPart1 = (int) substr($key1, 3, -1); // Extract numeric part of key1
        $numericPart2 = (int) substr($key2, 3, -1); // Extract numeric part of key2
        return $numericPart1 - $numericPart2; // Compare numeric parts
    }
    return $letterComparison;
});
0

Maybe this is the simplest way you can try:

<?php

$testArray = [
    "01-01A" => [
        'name' => "test1",
        'test' => "01-01A"
    ],
    "01-06B" => [
        'name' => "test2",
        'test' => "01-06B"
    ],
    "02-05A" => [
        'name' => "test3",
        'test' => "01-05A"
    ],
    "03-33B" => [
        'name' => "test4",
        'test' => "01-33B"
    ],
    "01-044B" => [
        'name' => "test5",
        'test' => "01-044B"
    ],
    "07-098A" => [
        'name' => "test1",
        'test' => "01-098A"
    ],
    "09-0963B" => [
        'name' => "test2",
        'test' => "01-0963B"
    ],
    "10-0596C" => [
        'name' => "test3",
        'test' => "01-0596C"
    ],
    "99-0586C" => [
        'name' => "test4",
        'test' => "01-0586C"
    ],
    "21-984E" => [
        'name' => "test5",
        'test' => "01-984E"
    ]
];

// get name as an separate array
$name = array_column($testArray, 'name');
// sorting $testArray ascending base on `name`
array_multisort($name, SORT_ASC, $testArray);

print_r($testArray);

and the result will be:

Array
(
    [01-01A] => Array
        (
            [name] => test1
            [test] => 01-01A
        )

    [07-098A] => Array
        (
            [name] => test1
            [test] => 01-098A
        )

    [01-06B] => Array
        (
            [name] => test2
            [test] => 01-06B
        )

    [09-0963B] => Array
        (
            [name] => test2
            [test] => 01-0963B
        )

    [10-0596C] => Array
        (
            [name] => test3
            [test] => 01-0596C
        )

    [02-05A] => Array
        (
            [name] => test3
            [test] => 01-05A
        )

    [99-0586C] => Array
        (
            [name] => test4
            [test] => 01-0586C
        )

    [03-33B] => Array
        (
            [name] => test4
            [test] => 01-33B
        )

    [01-044B] => Array
        (
            [name] => test5
            [test] => 01-044B
        )

    [21-984E] => Array
        (
            [name] => test5
            [test] => 01-984E
        )

)
2
  • How can I sort this array using a key or 'test' ?? Commented Aug 31, 2023 at 7:31
  • just need to change the name to test => // get name as an separate array $name = array_column($testArray, 'name'); // sorting $testArray ascending base on test array_multisort($name, SORT_ASC, $testArray); print_r($testArray);
    – Fiko
    Commented Sep 4, 2023 at 11:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.