drzrdc1766788 2015-05-18 07:02
浏览 61

PHP:数组int比较错误?

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

  • 写回答

3条回答 默认 最新

  • du4629 2015-05-18 07:20
    关注

    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;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测