1

您好,我刚刚完成了一个代码,其中我得到了 50 个变量......所有这些变量都有 int 值..

我将变量作为单独的值,仅在此示例中,我将使用结果设置变量,但结果来自其他评估和很好的东西,因为我已经回显了经过验证的结果。

$one = 13
$two = 35
$three = 46


The "item1" appears <?PHP echo $one; ?> times<br />
The "item2" appears <?PHP echo $two; ?> times<br />
The "item3" appears <?PHP echo $three; ?> times<br />

这很好,但是,我如何以 ASC 方式或 DSC 订购结果,以建立订单...

非常感谢

到目前为止,这很好用

$naturales = array(
  $uno => "n1",
  $dos => "n2",
  $tres => "n3",
  $cuatro => "n4",
  $cinco => "n5",
  $seis => "n6",
  $siete => "n7",
  $ocho => "n8",
  $nueve => "n9",
  $diez => "n10",
  $once => "n11",
  $doce => "n12",
  $trece => "n13",
  $catorce => "n14",
  $quince => "n15",
  $dieciseis => "n16",
  $diecisiete => "n17",
  $dieciocho => "n18",
  $diecinueve => "n19",
  $veinte => "n20",
  $veintiuno => "n21",
  $veintidos => "n22",
  $veintitres => "n23",
  $veinticuatro => "n24",
  $veinticinco => "n25",
  $veintiseis => "n26",
  $veintisiete => "n27",
  $veintiocho => "n28",
  $veintinueve => "n29",
  $treinta => "n30",
  $treintayuno => "n31",
  $treintaydos => "n32",
  $treintaytres => "n33",
  $treintaycuatro => "n34",
  $treintaycinco => "n35",
  $treintayseis  => "n36",
  $treintaysiete => "n37",
  $treintayocho => "n38",
  $treintaynueve => "n39",
  $cuarenta => "n40",
  $cuarentayuno => "n41",
  $cuarentaydos => "n42",
  $cuarentaytres => "n43",
  $cuarentaycuatro => "n44",
  $cuarentaycinco => "n45",
  $cuarentayseis => "n46",
  $cuarentaysiete => "n47",
  $cuarentayocho => "n48",
  $cuarentaynueve => "n49",
  $cincuenta => "n50",
  $cincuentayuno => "n51",
  $cincuentaydos => "n52",
  $cincuentaytres => "n53",
  $cincuentaycuatro => "n54",
  $cincuentaycinco => "n55",
  $cincuentayseis => "n56", 
);

krsort($naturales);

foreach ($naturales as $count => $name) {
  echo "The \"$name\" appears $count times<br />";
}

为什么我的结果是这样的(它隐藏了所有结果为 12(类似计数结果),例如“n3”出现了 12 次。它没有列出。

The "n20" appears 12 times
The "n30" appears 11 times
The "n37" appears 10 times
The "n41" appears 9 times
The "n42" appears 8 times
The "n45" appears 7 times
The "n47" appears 6 times
The "n35" appears 5 times
The "n44" appears 4 times
The "n46" appears 2 times
The "n56" appears 0 times
4

3 回答 3

3

构建一个数组

$myresults = array("Item1"=>13,"item2"=>35,"item3"=>46);

然后在数组 $myresults 上使用asort()orarsort()

然后做一个 for/foreach 循环来输出结果

基本准则,但除此之外,您应该能够在谷歌上搜索如何相当容易地详细实施(即使在这里也可以)

于 2013-05-21T16:01:01.247 回答
1

如前所述,您可以将值插入关联数组,即:

$items = array(
  $one => "item1",
  $two => "item2",
  $three => "item3"
);

然后您可以使用类似 ksort() 的函数对所有值进行排序:http: //php.net/manual/en/function.ksort.php

所以你可以得到这样的结果:

ksort($items);

foreach ($items as $count => $name) {
  echo "The \"$name\" appears $count times<br />";
}
于 2013-05-21T16:03:31.960 回答
1
$one = 13;
$two = 35;
$three = 46;

$arr = array("Item 1"=>$one,"Item 2"=>$two,"Item 3"=>$three);

echo "<strong>Original</strong><br />";
foreach($arr as $k => $v){
    echo $k . " = " . $v . "<br />";
}

asort($arr);
echo "<strong>Ascending Sort</strong><br />";
foreach($arr as $k => $v){
    echo $k . " = " . $v . "<br />";
}

arsort($arr);
echo "<strong>Descending Sort</strong><br />";
foreach($arr as $k => $v){
    echo $k . " = " . $v . "<br />";
}

如前所述,您可以根据需要使用asortarsort对数组进行排序......我在这里添加了一些示例以及一些工作代码

于 2013-05-21T16:14:12.370 回答