1

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!

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

I don't see [place] .. is that the problem?
@alex Yes we do. :o) @user Sorry, added the place. I guess the reason you're getting an empty array is that neither result is within the given timeframe.
1

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.

1 Comment

A sight for sore eyes. ;-) Unfortunately I'm still too tied to 5.2 for the moment... :-(
0

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.

1 Comment

Why I'm getting an empty array??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.