Php 无法对数组索引进行排序
我有一个这样的数组 输出:Php 无法对数组索引进行排序,php,codeigniter,codeigniter-3,Php,Codeigniter,Codeigniter 3,我有一个这样的数组 输出: Array ( [3] => stdClass Object ( [id] => 11591 [title] => abc ) [2] => stdClass Object ( [id] => 11592
Array
(
[3] => stdClass Object
(
[id] => 11591
[title] => abc
)
[2] => stdClass Object
(
[id] => 11592
[title] => xyz
)
[0] => stdClass Object
(
[id] => 11589
[title] => abg
)
[1] => stdClass Object
(
[id] => 11590
[title] => asw
)
)
代码点火器代码:
foreach($results as $rowData)
{
if($rowData->title=='xyz')
{
$eventperDayArray['0']=$rowData;
}
else if($rowData->title=='asw')
{
$eventperDayArray['1']=$rowData;
}
else if($rowData->title=='abc')
{
$eventperDayArray['2']=$rowData;
}
else if($rowData->title=='abg')
{
$eventperDayArray['3']=$rowData;
}
if($i==5)
{
print_r($eventperDayArray);
die();
break;
}
$i++;
}
我正在搜索数组中的数据,并希望对其进行排序。现在当我print\r($eventperDayArray)代码>它
我得到的输出是这样的,现在我想对它进行排序,这样0索引应该排在第一位,以此类推。
我使用了sort
和ksort
,但它不起作用。它打印1
使用ksort()按键排序
foreach($results as $rowData) {
if ($rowData->title == 'MORNING') {
$eventperDayArray['0'] = $rowData;
} else if ($rowData->title == 'AFTERNOON') {
$eventperDayArray['1'] = $rowData;
} else if ($rowData->title == 'EVENING') {
$eventperDayArray['2'] = $rowData;
} else if ($rowData->title == 'NIGHT') {
$eventperDayArray['3'] = $rowData;
}
if ($i == 5) {
print_r($eventperDayArray);
die();
break;
}
$i++;
}
ksort($eventperDayArray);
eventperDayArray
按数组上的键进行排序
<?php
$arr = [];
$o = new stdclass();
$o->id = 11591;
$o->title = 'abc';
$arr[3] = $o;
$o = new stdclass();
$o->id = 11592;
$o->title = 'xyz';
$arr[2] = $o;
$o = new stdclass();
$o->id = 11589;
$o->title = 'abg';
$arr[0] = $o;
$o = new stdclass();
$o->id = 11590;
$o->title = 'asw';
$arr[1] = $o;
ksort($arr);
print_r($arr);
ksort
对提供的数组进行排序,但不返回已排序的数组。使用它,然后查看$eventperDayData
中的内容。当您回显布尔值时,返回true
或1
表示它已完成排序。您希望实现什么?