我有以下数组:
Array
(
[0] => stdClass Object
(
[retailjobmaster_id] => 3233
[invoicedate] => 2014-05-01 10:10:33
[name] => Xerox 1000
)
[1] => stdClass Object
(
[retailjobmaster_id] => 3234
[retailjobsub_id] => 1
[invoicedate] => 2014-05-01 10:12:21
)
[2] => stdClass Object
(
[retailjobmaster_id] => 3236
[invoicedate] => 2014-05-01 10:46:47
[name] => Xerox 1000
)
[4] => stdClass Object
(
[retailjobmaster_id] => 3237
[invoicedate] => 2014-05-01 10:59:45
[name] => Xerox 1000
)
[5] => stdClass Object
(
[retailjobmaster_id] => 3243
[invoicedate] => 2014-05-01 11:49:33
[name] => Xerox 1000
)
[6] => stdClass Object
(
[retailjobmaster_id] => 3244
[invoicedate] => 2014-05-01 12:12:37
[name] => Xerox 1000
)
)
我想根据时间间隔将这个数组分成不同的数组,如:
[invoicedate] => 2014-05-01 09:30:00 &<= 2014-05-01 10:30:00 like wise时间间隔1小时。尝试根据时间间隔
进行排序Array
(
Array(
[0] => stdClass Object
(
[retailjobmaster_id] => 3233
[invoicedate] => 2014-05-01 10:10:33
[name] => Xerox 1000
)
[1] => stdClass Object
(
[retailjobmaster_id] => 3234
[retailjobsub_id] => 1
[invoicedate] => 2014-05-01 10:12:21
)
)
Array (
[0] => stdClass Object
(
[retailjobmaster_id] => 3236
[invoicedate] => 2014-05-01 10:46:47
[name] => Xerox 1000
)
[1] => stdClass Object
(
[retailjobmaster_id] => 3237
[invoicedate] => 2014-05-01 10:59:45
[name] => Xerox 1000
)
)
Array (
[0] => stdClass Object
(
[retailjobmaster_id] => 3243
[invoicedate] => 2014-05-01 11:49:33
[name] => Xerox 1000
)
[1] => stdClass Object
(
[retailjobmaster_id] => 3244
[invoicedate] => 2014-05-01 12:12:37
[name] => Xerox 1000
)
)
)
我该怎么做呢?谢谢。
试试这个($tmp应该是包含你在你的帖子中显示的stdClass的变量):
function objectToArray($d) {
if (is_object($d)) {
$d = get_object_vars($d);
}
if (is_array($d)) {
return array_map(__FUNCTION__, $d);
} else {
return $d;
}
}
function groupByHour($array) {
$return = array();
$transformed = array();
foreach($array AS $value) {
array_push($transformed,objectToArray($value));
}
foreach($transformed AS $value) {
$time = date("G",strtotime($value['invoicedate']));
if(!isset($return[$time])) {
$return[$time] = array();
}
$return[$time][] = $value;
}
return $return;
}
echo '<pre>';
print_r(groupByHour($tmp));
echo '</pre>';
结果将是一个数组,其中在第一级,作为键,您将拥有发票的小时数(10,11,12),在第二级是在该小时创建的每个条目的数组。(对于你的帖子中的例子)