数组php的排序数组
我正在从事一个基于codeigniter的小项目,我不是php开发人员,这是我的问题:数组php的排序数组,php,arrays,codeigniter,associative,Php,Arrays,Codeigniter,Associative,我正在从事一个基于codeigniter的小项目,我不是php开发人员,这是我的问题: foreach ($checkeds['id_iscritti'] as $checked){ $iscritto = $this->pellegrinaggio_iscritti_m->get_iscritto($checked); $utente = $this->utenti_m->get_utente($iscritto[0]-
foreach ($checkeds['id_iscritti'] as $checked){
$iscritto = $this->pellegrinaggio_iscritti_m->get_iscritto($checked);
$utente = $this->utenti_m->get_utente($iscritto[0]->id_utente);
echo ("utente: <pre> ");var_dump($utente);echo (" </pre> \n\n");
}
如何按索引“nome”对数组$utenti进行排序?
我花了几个小时来了解这种阵列是如何工作的,没有任何结果,你能帮我吗?这就是你想要的功能: 您的代码应该如下所示:
usort($utente, function($a, $b){
if ($a->nome > $b->nome) {
return 1;
}elseif($a->nome < $b->nome){
return -1;
}else{
return 0;
}
});
PHPs usort()函数允许您使用自定义函数对数组进行排序
假设您的对象数组存储在$ute
中,下面将使用匿名比较函数将每个对象与其他对象进行比较
在下面的代码中,$utete
数组将按nome
值升序排序
usort($utete,function($a,$b){
如果($a->nome>$b->nome){
返回1;
}elseif($a->nome<$b->nome){
返回-1;
}否则{
返回0;
}
});
有关更多详细信息,请访问如果我错了,请纠正我的错误,但我确信
array\u multisort()
只对数组进行排序,而不是对对象数组进行排序?看来你是对的,我错过了。另一个答案(你的)是正确的。
$sorted = array_multisort($utente, 'nome', SORT_ASC);
usort($utente, function($a, $b){
if ($a->nome > $b->nome) {
return 1;
}elseif($a->nome < $b->nome){
return -1;
}else{
return 0;
}
});