简体   繁体   中英

split array into multiple array based in date time php

I have the following 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
    )

)

I want to split this array into different arrays based on time interval like if:

[invoicedate] => 2014-05-01 09:30:00 && <= 2014-05-01 10:30:00 like wise time interval 1 hour . trying to sort based on time interval

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
    )
    )

)

How would I go about doing this? Thanks.

Try this ($tmp should be the variable that contains the stdClass you have showed in your post) :

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>';

The result will be an array where at the first level, as keys, you will have the hour of the invoice (10,11,12) and on the 2nd level an array of each of the entries that were created at that hour. (for the example you had in your post)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM