我試過sort
,Ksort
,multiSort
,沒有任何工作,我不知道爲什麼。我可以使用print_r
,看到它是一個數組,但它不會排序只是不斷給出錯誤。我認爲這是因爲值是floats
,但我可能是錯的。如何對數組中的浮點數進行排序?
下面是使用print_r的功能與陣列頁面顯示:
這裏是我的代碼:
<?php
$uploads = wp_upload_dir(); //Path to my gallery uploads folder
if ($dir = opendir($uploads['basedir'].'/gallery-2')) {
$images = array();
while (false !== ($file = readdir($dir))) {
if ($file != "." && $file != "..") {
$images[] = $file;
}
}
closedir($dir);
}
$images = ksort($images); /* not working */
// echo '<pre>';
// echo print_r($images);
// echo '</pre>';
foreach($images as $image) {
echo '<figure><img src="';
echo $uploads['baseurl'].'/gallery-2/'. $image;
echo '" alt="" /></li>';
echo '<figcaption>';
echo '<p>' . erq_shortcode() . '</p>';
echo '</figcaption>';
echo '</figure>';
}
?>
'ksort'通過引用更改數組並返回一個布爾值(成功時爲true,失敗時爲false),因此您不應將其保存到'$ images'中。只要做'ksort($ images);'。 – 2014-10-26 16:28:13
這些值不是FLOATS - 帶圖像名稱的簡單字符串。 sort()應該在這裏正常工作,只要$ images確實是你的打印屏幕顯示的數組。 – andrew 2014-10-26 16:29:12
我忘了提及在這個腳本中一切正常,直到我嘗試對它進行排序,那麼它只給出錯誤。我需要能夠對這些圖像進行排序,以便我可以在文件夾中將它們命名爲1.png,2.png,3.png等。 – 2014-10-26 16:29:57