簡體   English   中英

PHP中的array_multisort問題

[英]array_multisort issue in PHP

我正在使用此行基於名稱字段以升序對二維數組進行排序。

 array_multisort($contact[0]['name'],SORT_DESC,$contact[0]['image'],$contact[0]['url'],$contact[0]['catimg'],$contact[0]['count']);

但是有時它不能正確排序。

里面怎么了?

謝謝

如果要使用array_multisort進行工作,則必須創建數據列,其外觀如下所示:

$names = array();
$images = array();
$urls = array();
// ... add more as needed
// make rows of data for sorting
foreach ($contact as $contact_row) {
    $names[]  = $contact_row['name'];
    $images[] = $contact_row['image'];
    $urls[]   = $contact_row['url'];
    // ... add more as needed
}
array_multisort($names, SORT_DESC, $images, $urls, $contact); // the last one is the array you want to sort

var_export($contact); // at this point this should be ordered

似乎,多數民眾贊成在傳遞給函數的參數不是數組。

嘗試使用usort:

usort($contact, function($a, $b){
    return strcmp($a['name'], $b['name']);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM