I don't know why I cannot get the expected result. The result shows 2 > 10 > 1 but I want 10 > 2 > 1 Can anyone help me? Thanks
<?
function reorder($a, $b)
{
$a = substr($a, strlen("a/"));
$a = substr($a, 0, -strlen(".php"));
$b = substr($b, strlen("a/"));
$b = substr($b, 0, -strlen(".php"));
switch ($a) {
case "1m_microUSB_cable":
$a['order'] = 1;
break;
case "25cm_microUSB_cable":
$a['order'] = 2;
break;
case "30cm_lightning_cable":
$a['order'] = 3;
break;
case "1.5m_lightning_cable":
$a['order'] = 5;
break;
case "1m_lightning_cable":
$a['order'] = 4;
break;
case "1m_microUSB_upgrade_cable":
$a['order'] = 6;
break;
case "hand_ring_cable_Android":
$a['order'] = 7;
break;
case "hand_ring_cable_Apple":
$a['order'] = 8;
break;
case "Light_bulb_key_ring":
$a['order'] = 9;
break;
case "candy_machine":
$a['order'] = 10;
break;
default:
$a['order'] = 999;
}
switch ($b) {
case "1m_microUSB_cable":
$b['order'] = 1;
break;
case "25cm_microUSB_cable":
$b['order'] = 2;
break;
case "30cm_lightning_cable":
$b['order'] = 3;
break;
case "1.5m_lightning_cable":
$b['order'] = 5;
break;
case "1m_lightning_cable":
$b['order'] = 4;
break;
case "1m_microUSB_upgrade_cable":
$b['order'] = 6;
break;
case "hand_ring_cable_Android":
$b['order'] = 7;
break;
case "hand_ring_cable_Apple":
$b['order'] = 8;
break;
case "Light_bulb_key_ring":
$b['order'] = 9;
break;
case "candy_machine":
$b['order'] = 10;
break;
default:
$b['order'] = 999;
}
if ($a['order'] == $b['order']) {
return 0;
} elseif ($a['order'] > $b['order']) {
return -1;
} else {
return 1;
}
}
?>
I want to reorder the array and I use
$glob = glob("a/*.php");
include ("reorder.php");
usort($glob, "reorder");
foreach ($glob as $filename) {
include ($filename);
include ("templates/a.php");
}
The dump of $glob:
array ( 0 => 'a/Light_bulb_key_ring.php', 1 => 'a/hand_ring_cable_Apple.php', 2 => 'a/hand_ring_cable_Android.php', 3 => 'a/1m_microUSB_upgrade_cable.php', 4 => 'a/1.5m_lightning_cable.php', 5 => 'a/1m_lightning_cable.php', 6 => 'a/30cm_lightning_cable.php', 7 => 'a/25cm_microUSB_cable.php', 8 => 'a/candy_machine.php', 9 => 'a/1m_microUSB_cable.php', )
I reorder the array from 1 to 9 is ok but when there is order 10, the order 10 will be after the order 1 but not the order 9. I don't know why? I hope someone can help me. Thank you! Sorry for my poor English.
I am so sorry that I type it unclearly. Therefore, I create a image.
The order now: Image 1
The expected order: Image 2
The given result is a lexical order, not a numeric order. To force numeric order, change:
$aNum = intval($a['order']);
$bNum = intval($b['order']);
return $bNum - $aNum;
I don't quite understand your sorting function logic. Any filename with value other then "test0", "test1" and "test2" will have same highest order "999".
If all of your filenames have "test<[\d]+>.php" format then you can simply extract that value and use that for the comparison:
function reorder($a, $b)
{
if (preg_match('|/a/test(\d+).php|', $a, $match)) {
$a = (int) $match[1];
} else {
$a = 999; // Filename is in different format so place it at the end of the list
}
if (preg_match('|/a/test(\d+).php|', $b, $match)) {
$b = (int) $match[1];
} else {
$b = 999;
}
return $a - $b;
}
$files = Array('/a/extra.php', '/a/test0.php', '/a/test1.php', '/a/test5.php', '/a/test10.php', '/a/test11.php', '/a/test100.php', '/a/test22.php',);
usort($files, 'reorder');
var_dump($files);
This ouputs:
array(8) {
[0]=>
string(12) "/a/test0.php"
[1]=>
string(12) "/a/test1.php"
[2]=>
string(12) "/a/test5.php"
[3]=>
string(13) "/a/test10.php"
[4]=>
string(13) "/a/test11.php"
[5]=>
string(13) "/a/test22.php"
[6]=>
string(14) "/a/test100.php"
[7]=>
string(11) "/a/extra.php"
}
Finally, I found the answer. It is a very silly mistake.
I would like to say thank you to all of the people who help me here. Thanks!
$a['order'] and $b['order'] is not work properly because the $a and $b are string (with content). As a result, $a['order'] will change the first letter(/digit) of the string $a. ($b same problem)
For example, $a = "this_is_string" will be replaced to $a = "2his_is_string" if $a['order'] = 2. So, if $a['order'] = 10 (any two or more digits), $a will become "1his_is_string" and the order will be 1 but not 10. Therefore, this error occurred.
To solve this problem, just change all $a['order'] to $a_order (or any other variables) and $b['order'] to $b_order (or any other variables).
(Sorry for my poor English)