php 对字母排序,PHP按字母顺序排序

在MySQL中:

SELECT * FROM table ORDER BY name ASC

在PHP中:

$fruits = array("lemon", "orange", "banana", "apple");

sort($fruits);

foreach ($fruits as $key => $val) {

echo "fruits[" . $key . "] = " . $val . "n";

}

fruits[0] = apple

fruits[1] = banana

fruits[2] = lemon

fruits[3] = orange

他似乎没有关于storting的问题,而是为每个新字母做列格式和标题。

假设$ arr包含带有数字键的按字母顺序排序的列表。每个元素都有索引'name'和'link'。对于来自SQL查询的数据,这应该是非常安全的假设。

$firstLetter = -1;

$desiredColumns = 4; //you can change this!

$columnCount = (count($arr)+27)/$desiredColumns+1;

echo "

";

foreach($arr as $key => $cur)

{

if ($key != 0 && $key % desiredColumns == 0) echo "

";

if ($cur['name'][0] !== $firstLetter)

{

echo "$firstLetter
"; $firstLetter = $cur['name'][0];

}

echo "".$cur['name']."
";

}

echo "

";

您必须将数字视为一种特殊情况,但这是个主意。如果你使用模板引擎,显然有更好的方法,但我想你会提到这一点。这是一个粗略的草图,使漂亮的HTML不是我的事情。

--Query-- get table into $arr. I can't see your tables obviously, Im making assumptions if names nad stuff so you'll need to verify or change them

$sql = "SELECT * FROM table T ORDER BY name";

$co

你可能感兴趣的:(php,对字母排序)