简体   繁体   English

PHP中的array_multisort问题

[英]array_multisort issue in PHP

I am using this line to sort my two dimensional array in ascending order based on name field. 我正在使用此行基于名称字段以升序对二维数组进行排序。

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

But some times its not sorting correctly. 但是有时它不能正确排序。

Whats wrong in it ? 里面怎么了?

Thanks 谢谢

You will have to make columns of data if you want to use array_multisort to work, in outline it will look like this: 如果要使用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

Seems, thats arguments passed to function are not arrays. 似乎,多数民众赞成在传递给函数的参数不是数组。

Try use usort: 尝试使用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