比较$ Variables Results,然后以不同的方式进行orden

时间:2013-05-21 15:55:51

标签: php sql sorting

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

我将变量作为separete值,只是对于这个例子,我将使用结果设置变量,但结果来自其他评估和其他东西很好,导致我已经回复了验证结果。

$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(类似计数结果) 例如,对于&#34; n3&#34;出现了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

3 个答案:

答案 0 :(得分:3)

构建数组

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

然后在数组$ myresults

上使用asort()arsort()

然后执行for / foreach循环以输出结果

基本指南但是你应该能够谷歌如何相当容易地详细实施(即使在这里也会有效)

答案 1 :(得分: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 />";
}

答案 2 :(得分: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对数组进行排序...我在这里添加了一些示例以及一些正在运行的 CODE