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