基于日期时间php将数组拆分为多个数组

时间:2014-05-29 07:14:16

标签: php arrays codeigniter multidimensional-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
    )

[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喜欢明智的时间间隔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
    )
    )

我该怎么做呢?感谢。

1 个答案:

答案 0 :(得分:0)

试试这个($ 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),在第二级,将创建在此处创建的每个条目的数组。小时。 (对于你在帖子中的例子)