0

这是我的对象数组:

Array (
[0] => stdClass Object ( [id] => 64 [name] => January [alias] => January [description] => 01)
[0] => stdClass Object ( [id] => 64 [名称] => 一月 [别名] => 一月 [描述] => 01)
[1] => stdClass Object ([id] => 65 [名称] => 二月 [别名] => 二月 [描述] => 02 )
...

我怎么能从当月排序,然后自然的方式,所以今天它会是:10 月,11 月,12 月,1 月 ..

所以对于这个例子,它将是:

Array (
[0] => stdClass Object ( [id] => 73 [name] => October [alias] => october [description] => 10)
[0] => stdClass Object ( [id] => 64 [名称] => 一月 [别名] => 一月 [描述] => 01)
[1] => stdClass Object ( [id] => 74 [名称] => 十一月 [别名] => 十一月 [描述] => 11 ) ...

我不知道该怎么做。

4

2 回答 2

0

Just check if month "smaller" than now then put it into end of array:

$arr[] = array('name'=>'jan', 'desc' => '01');
$arr[] = array('name'=>'feb', 'desc' => '02');
$arr[] = array('name'=>'mar', 'desc' => '03');
$arr[] = array('name'=>'apr', 'desc' => '04');
$arr[] = array('name'=>'may', 'desc' => '05');
$arr[] = array('name'=>'jun', 'desc' => '06');
$arr[] = array('name'=>'jul', 'desc' => '07');
$arr[] = array('name'=>'aug', 'desc' => '08');
$arr[] = array('name'=>'sep', 'desc' => '09');
$arr[] = array('name'=>'oct', 'desc' => '10');
$arr[] = array('name'=>'nov', 'desc' => '11');
$arr[] = array('name'=>'dec', 'desc' => '12');

foreach($arr as $i=>$y){
    if($y['desc'] != date('m')){
        unset($x[$i]);
        $arr[] = $y;
    }
    else{
        break;
    }
}
$arr = array_values($arr);
于 2013-10-16T07:06:40.753 回答
0

如果你的对象数组总是以 jan 开头,每个月有 12 个元素,试试这个,这可能不是最有效的方法:

$curMonth = date("n")-1;
$out = array();

for($i = 0; $i < 12; $i++)
  {
  $index = ( $i + $curMonth ) % 12;
  $out[$i] = $YourObjectArray[$index]
  }

print_r($out);
于 2013-10-16T07:01:51.470 回答