【发布时间】:2010-03-27 09:45:10
【问题描述】:
我的代码有问题,希望有人能够解决。主要目的是根据数组的值对数组进行排序(然后重新索引其数字键)。
我得到了这个文件名样本:
$filename = array("index 198.php", "index 192.php", "index 144.php", "index 2.php", "index 1.php", "index 100.php", "index 111.php");
$alloutput = array(); //all of index in array
foreach ($filename as $name) {
preg_match('#(\d+)#', $name, $output); // take only the numerical from file name
array_shift($output); // cleaned. the last code create duplicate numerical in $output,
if (is_array($output)) {
$alloutput = array_merge($alloutput, $output);
}
}
//try to check the type of every value in array
foreach ($alloutput as $output) {
if (is_array($output)) {
echo "array true </br>";
} elseif (is_int($output)) {
echo "integer true </br>";
} elseif (is_string($output)) { //the numerical taken from filename always resuld "string".
echo "string true </br>";
}
}
这段代码的输出将是:
大批 ( [0] => 198 [1] => 192 [2] => 144 [3] => 2 [4] => 1 [5] => 100 [6] => 111 )
我已经测试了数组中的每个输出。 都是字符串(不是数字),所以问题是如何把这个字符串改成整数,这样我可以把它从小到大排序? p>
这段代码的主要目的是如何输出从低到高排序的数组?
【问题讨论】:
-
我看不懂你的代码,你的循环中有未定义的变量(hasil 和 hasilku)
-
nicolo,对此我感到非常抱歉。我会尝试改变它