php按字母顺序对多维数组进行排序
我有一个多维数组,它是用以下方法创建的:php按字母顺序对多维数组进行排序,php,arrays,sorting,Php,Arrays,Sorting,我有一个多维数组,它是用以下方法创建的: for($i=0; $i<count($sub_results);$i++) { $presenters = array( 'first_name' => $sub_results[$i]['data'][1]['user_value'], 'last_name' => $sub_results[$i]['data'][2]['user_value'], 'preferred_na
for($i=0; $i<count($sub_results);$i++)
{
$presenters = array(
'first_name' => $sub_results[$i]['data'][1]['user_value'],
'last_name' => $sub_results[$i]['data'][2]['user_value'],
'preferred_name' => $sub_results[$i]['data'][8]['user_value'],
'organisation' => $sub_results[$i]['data'][11]['user_value'],
'portraitFile' => $portrait_file_path.$portrait,
'Abstract1title' => $sub_results[$i]['data'][19]['user_value'],
'Abstract1accepted' => $sub_results[$i]['data'][18]['user_value'],
'Abstract1synopsis' => $sub_results[$i]['data'][22]['user_value'],
'Abstract1format' => $sub_results[$i]['data'][26]['user_value'],
'Abstract1coauthors' => $sub_results[$i]['data'][20]['user_value'],
'Abstract2title' => $sub_results[$i]['data'][28]['user_value'],
'Abstract2accepted' => $sub_results[$i]['data'][27]['user_value'],
'Abstract2synopsis' => $sub_results[$i]['data'][31]['user_value'],
'Abstract2format' => $sub_results[$i]['data'][30]['user_value'],
'Abstract2coauthors' => $sub_results[$i]['data'][29]['user_value']
);
}
对于($i=0;$i$sub_结果[$i]['data'][1]['user_值'],
'last_name'=>$sub_结果[$i]['data'][2]['user_值'],
“首选\u名称”=>$sub\u结果[$i]['data'][8]['user\u值'],
“组织”=>$sub_结果[$i]['data'][11]['user_值'],
“肖像文件”=>$RATIATION\u文件\u路径。$RATIATION,
“Abstract1title”=>$sub_结果[$i]['data'][19]['user_值'],
“Abstract1accepted”=>$sub_结果[$i]['data'][18]['user_值'],
“Abstract1synopsis”=>$sub_结果[$i]['data'][22]['user_值'],
“Abstract1format”=>$sub_结果[$i]['data'][26]['user_值'],
“Abstract1coauthors”=>$sub_结果[$i]['data'][20]['user_值'],
“Abstract2title”=>$sub_结果[$i]['data'][28]['user_值'],
'Abstract2accepted'=>$sub_结果[$i]['data'][27]['user_值'],
“Abstract2synopsis”=>$sub_结果[$i]['data'][31]['user_值'],
“Abstract2format”=>$sub_结果[$i]['data'][30]['user_值'],
'Abstract2coauthors'=>$sub_结果[$i]['data'][29]['user_值']
);
}
我想按姓氏的字母顺序对结果数组进行排序。这是如何实现的
事先致谢,,
emrys这应该能帮到你。它不区分大小写。如果你对它的工作原理有任何疑问,请告诉我。请记住,
usort
通过引用接受数组,这意味着如果要保留原始数组,则需要复制它
<?php
usort($array,function($a,$b) {
$comparison = strcasecmp($a['last_name'],$b['last_name']);
if ($comparison == 0) return 0;
return ($comparison < 0) ? -1 : 1;
});
?>
生成的数组是什么?循环只是不断覆盖同一个变量,而不是附加到数组中。你的意思是写$presenters[]=
?无论如何,最明显的答案是使用usort
。提供比较姓氏的比较函数。如果这对您不起作用,请显示您的代码,我们将帮助您修复它。示例#2我使用usort回答了您下面的问题。如果你有任何问题,请告诉我。太好了,谢谢。也感谢Barmar指出了我的控制循环中一个非常明显的缺陷。