PHP:从数组中获取时间戳数据

发布于 2024-11-01 22:28:49 字数 1286 浏览 1 评论 0原文

这里的一个 php 学生现在已经被困了好几个小时了:(

这是我的问题,这个:

   echo"<pre>";print_r($array1);echo"</pre>";

prints:

    Array
    (
        [0] => stdClass Object
            (
                [id] => 4d6f6aec35993704d52c0d9d
                [createdAt] => 1299147500
                [place] => stdClass Object
                    (
                        [id] => 4adcda40f964a5208a3e21e3
                    )


            )

                [1] => stdClass Object
            (
                [id] => 654jk654n646g54j6kl54j645
                [createdAt] => 1299147500
                [place] => stdClass Object
                    (
                        [id] => 4gh543gh5h5g354h3gg53gh
                    )


            )
.
.
.

createdAt 是该地方创建日期的时间戳。 我需要在时间戳之间创建的地点的地点 ID。

这是我对时间戳之间逻辑的方法:

$array2 = array();

$begin = strtotime("2011-02-17 12:22:49");
$end = strtotime("2011-03-03 10:00:00");

foreach($array1 as $timestamp){
    if($timestamp <= $end && $timestamp >= $begin){
                $array2[] = $timestamp;
    }

}

这对我来说是正确的,但正如我所说,我在 $array2 中需要的信息是在这些时间戳之间创建的位置 id。

我怎样才能做到这一点?

非常感谢!

A php student here that has been stuck for many hours now :(

Here's my problem, this:

   echo"<pre>";print_r($array1);echo"</pre>";

prints:

    Array
    (
        [0] => stdClass Object
            (
                [id] => 4d6f6aec35993704d52c0d9d
                [createdAt] => 1299147500
                [place] => stdClass Object
                    (
                        [id] => 4adcda40f964a5208a3e21e3
                    )


            )

                [1] => stdClass Object
            (
                [id] => 654jk654n646g54j6kl54j645
                [createdAt] => 1299147500
                [place] => stdClass Object
                    (
                        [id] => 4gh543gh5h5g354h3gg53gh
                    )


            )
.
.
.

createdAt is the timestamp of the date the place was created.
I need the place id of the places created in between timestamps.

Here's my approach on the in between timestamps logic:

$array2 = array();

$begin = strtotime("2011-02-17 12:22:49");
$end = strtotime("2011-03-03 10:00:00");

foreach($array1 as $timestamp){
    if($timestamp <= $end && $timestamp >= $begin){
                $array2[] = $timestamp;
    }

}

It is correct to me, but as I said the info I need in $array2 is the place ids created in between these timestamps.

How can I do that?

Thanks a ton!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最丧也最甜 2024-11-08 22:28:49

只是想在这里变得聪明......;-)

$begin = strtotime("2011-02-17 12:22:49");
$end = strtotime("2011-03-03 10:00:00");

$array2 = array_filter($array1, create_function('$o', "return \$o->createdAt >= $begin && \$o->createdAt <= $end;"));
$array2 = array_map(create_function('$o', 'return $o->place->id;'), $array2);

Just trying to be clever here... ;-)

$begin = strtotime("2011-02-17 12:22:49");
$end = strtotime("2011-03-03 10:00:00");

$array2 = array_filter($array1, create_function('$o', "return \$o->createdAt >= $begin && \$o->createdAt <= $end;"));
$array2 = array_map(create_function('$o', 'return $o->place->id;'), $array2);
疑心病 2024-11-08 22:28:49

对于 PHP >= 5.3 中的闭包来说,这将是一个很好的用例:

$begin = strtotime("2011-02-17 12:22:49");
$end   = strtotime("2011-03-03 10:00:00");

$array2 = array_map(function($item) {
    return $item->place->id;
}, array_filter($array1, function($item) use($begin, $end) {
    return $item->createdAt >= $begin && $item->createdAt <= $end;
}));

本质上它与 deceze,但我们使用较新的闭包代替旧式匿名函数从 PHP 5.3 开始。

This would be a nice use-case for closures in PHP >= 5.3:

$begin = strtotime("2011-02-17 12:22:49");
$end   = strtotime("2011-03-03 10:00:00");

$array2 = array_map(function($item) {
    return $item->place->id;
}, array_filter($array1, function($item) use($begin, $end) {
    return $item->createdAt >= $begin && $item->createdAt <= $end;
}));

Essentially it's the same code as shown by deceze, but instead of the old-style anonymous functions we use the newer closures from PHP 5.3.

倾城花音 2024-11-08 22:28:49

如果我正确理解这个问题,我想你会想要这样做。

$array2 = array();

$begin = strtotime("2011-02-17 12:22:49");
$end = strtotime("2011-03-03 10:00:00");

foreach($array1 as $item){
    $timestamp = $item->createdAt;
    if($timestamp <= $end && $timestamp >= $begin){
                $array2[] = $item->place;
    }

}

另外,为了让您知道您可以这样做:

echo '<pre>' . print_r($array2, 1) . '</pre>';

在调试时节省一些击键次数。

I think you want to do this if I understand the question correctly.

$array2 = array();

$begin = strtotime("2011-02-17 12:22:49");
$end = strtotime("2011-03-03 10:00:00");

foreach($array1 as $item){
    $timestamp = $item->createdAt;
    if($timestamp <= $end && $timestamp >= $begin){
                $array2[] = $item->place;
    }

}

Also just so you know you can do:

echo '<pre>' . print_r($array2, 1) . '</pre>';

to save your self some key strokes when debugging.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文