【问题标题】:PHP: array int compare error?PHP:数组int比较错误?
【发布时间】:2015-07-29 14:06:57
【问题描述】:

我不知道为什么我不能得到预期的结果。 结果显示 2 > 10 > 1 但我想要 10 > 2 > 1 谁能帮我?谢谢

<?
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;
        }
    }
?>

我想重新排序数组,我使用

$glob = glob("a/*.php");
include ("reorder.php");
usort($glob, "reorder");
foreach ($glob as $filename) {
        include ($filename);
        include ("templates/a.php");
    }

$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', )

我将数组从 1 重新排序到 9 是可以的,但是当有订单 10 时,订单 10 将在订单 1 之后而不是订单 9。我不知道为什么?我希望有一个人可以帮助我。谢谢! 对不起我的英语不好。

很抱歉,我写得不清楚。因此,我创建了一个图像。

现在的订单: Image 1

预期顺序: Image 2

【问题讨论】:

  • 其实,我怕我完全误解了你。您能否发布 $glob 的确切内容、您期望的确切顺序以及对该顺序的解释?
  • $glob = glob("a/*.php");我的预期顺序:(文件名的自定义顺序)例如:test0.php = order 1 test1.php = order 2 etc...
  • 删除所有ifs并写return $a['order'] - $b['order'];
  • 或者使用 natsort() - php.net/manual/en/function.natsort.php
  • return $b['order'] - $a['order']; 还是返回上面的结果...

标签: php arrays int compare


【解决方案1】:

给定的结果是词汇顺序,而不是数字顺序。要强制数字顺序,请更改:

    $aNum = intval($a['order']);
    $bNum = intval($b['order']);

    return $bNum - $aNum;

【讨论】:

  • “它不工作”是什么意思(除了它拼写“它不工作”)?结果如何?
【解决方案2】:

我不太了解您的排序功能逻辑。任何除“test0”、“test1”和“test2”之外的值的文件名都将具有相同的最高顺序“999”。

如果您所有的文件名都具有“test.php”格式,那么您可以简单地提取该值并将其用于比较:

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);

这个输出:

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"
}

【讨论】:

    【解决方案3】:

    终于,我找到了答案。这是一个非常愚蠢的错误。

    我要感谢所有在这里帮助我的人。谢谢!

    $a['order'] 和 $b['order'] 不能正常工作,因为 $a 和 $b 是字符串(有内容)。结果,$a['order'] 将更改字符串 $a 的第一个字母(/数字)。 ($b同样的问题)

    例如,如果 $a['order'] = 2,$a = "this_is_string" 将被替换为 $a = "2his_is_string"。所以,如果 $a['order'] = 10(任意两个或更多位),$a 将变为“1his_is_string”,顺序为 1 而不是 10。因此,出现此错误。

    要解决这个问题,只需将所有 $a['order'] 更改为 $a_order(或任何其他变量),并将 $b['order'] 更改为 $b_order(或任何其他变量)即可。强>

    (对不起我的英语不好)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-06
      • 1970-01-01
      相关资源
      最近更新 更多