Home > Mobile >  Sort PHP Array By SubString in Array of URLs
Sort PHP Array By SubString in Array of URLs

Time:02-15

I have the following list of URLs in an array (truncated):

array(20) { 
[0]=> string(76) "gfx/06_tension_anom.png" 
[1]=> string(77) "gfx/102_tension_anom.png" 
[2]=> string(77) "gfx/108_tension_anom.png" 
[3]=> string(77) "gfx/114_tension_anom.png" 
[4]=> string(77) "gfx/120_tension_anom.png" 
[5]=> string(76) "gfx/12_tension_anom.png" 
[6]=> string(76) "gfx/18_tension_anom.png" 
[7]=> string(76) "gfx/24_tension_anom.png" 
[8]=> string(76) "gfx/30_tension_anom.png" 
[9]=> string(76) "gfx/36_tension_anom.png" 
[10]=> string(76) "gfx/42_tension_anom.png" 
[11]=> string(76) "gfx/48_tension_anom.png" 
... }

When I display these in order, they're displayed in the string order, 102, 108, 114, 120 are out of order. How do I sort these URLs by numeric order returned in a different array? The numbers will always be after "gfx". Also, the numbers don't stop at 120 in reality, they can go up to whatever number.

I tried several functions array_map, sort, asort, and even some regex to no avail.

Thank you in advance.

CodePudding user response:

function test1($n) {
    preg_match('/\/([0-9] )_/', $n, $output_array);
    return str_repeat("x", strlen($output_array[1])).' '.$n;
}
function test2($n) {
    $z = explode(" ", $n);
    return $z[1];
}

$a = array('gfx/102_tension_anom.png', 'gfx/06_tension_anom.png', 'gfx/16_tension_anom.png');

$b = array_map('test1', $a);
asort($b);
$b = array_values(array_map('test2', $b));
print_r($b);


//result
Array
(
[0] => gfx/06_tension_anom.png
[1] => gfx/16_tension_anom.png
[2] => gfx/102_tension_anom.png
)

hope it helps you.

CodePudding user response:

PHP has a simple way to solve this for you: https://www.php.net/manual/en/function.natsort.php

<?php

$fileNames = [
    "gfx/06_tension_anom.png",
    "gfx/102_tension_anom.png",
    "gfx/108_tension_anom.png",
    "gfx/114_tension_anom.png",
    "gfx/120_tension_anom.png",
    "gfx/12_tension_anom.png",
    "gfx/18_tension_anom.png",
    "gfx/24_tension_anom.png",
    "gfx/30_tension_anom.png",
    "gfx/36_tension_anom.png",
    "gfx/42_tension_anom.png",
    "gfx/48_tension_anom.png"
 ];

natsort($fileNames);
var_dump($fileNames);

// array(12) {
//     [0]=>
//     string(23) "gfx/06_tension_anom.png"
//     [5]=>
//     string(23) "gfx/12_tension_anom.png"
//     [6]=>
//     string(23) "gfx/18_tension_anom.png"
//     [7]=>
//     string(23) "gfx/24_tension_anom.png"
//     [8]=>
//     string(23) "gfx/30_tension_anom.png"
//     [9]=>
//     string(23) "gfx/36_tension_anom.png"
//     [10]=>
//     string(23) "gfx/42_tension_anom.png"
//     [11]=>
//     string(23) "gfx/48_tension_anom.png"
//     [1]=>
//     string(24) "gfx/102_tension_anom.png"
//     [2]=>
//     string(24) "gfx/108_tension_anom.png"
//     [3]=>
//     string(24) "gfx/114_tension_anom.png"
//     [4]=>
//     string(24) "gfx/120_tension_anom.png"
// }

// NOTE: keys are messed up after natsort()
// If you want array indexed correctly then you also need:

$fileNames = array_values($fileNames);
var_dump($fileNames);

// array(12) {
//     [0]=>
//     string(23) "gfx/06_tension_anom.png"
//     [1]=>
//     string(23) "gfx/12_tension_anom.png"
//     [2]=>
//     string(23) "gfx/18_tension_anom.png"
//     [3]=>
//     string(23) "gfx/24_tension_anom.png"
//     [4]=>
//     string(23) "gfx/30_tension_anom.png"
//     [5]=>
//     string(23) "gfx/36_tension_anom.png"
//     [6]=>
//     string(23) "gfx/42_tension_anom.png"
//     [7]=>
//     string(23) "gfx/48_tension_anom.png"
//     [8]=>
//     string(24) "gfx/102_tension_anom.png"
//     [9]=>
//     string(24) "gfx/108_tension_anom.png"
//     [10]=>
//     string(24) "gfx/114_tension_anom.png"
//     [11]=>
//     string(24) "gfx/120_tension_anom.png"
// }
  • Related