开发者

How to assign $arr_date to a while loop?

This is the result of $arr_date.

Array
(
    [0] => stdClass Object
        (
            [date] => 2008-08-20
            [page_views] => 2
        )

    [1] => stdClass Object
        (
            [date] => 2011-05-03
            [page_views] => 1
        )

    [2] => stdClass Object
        (
            [date] => 2011-09-05
            [page_views] => 4
        )

    [3] => stdClass Object
        (
            [date] => 2011-10-11
            [page_views] => 2
        )

)

How to put my array $arr_date here in the while loop? This is not working yet.

while (true) {
        $page_ctr++;
        if (date('Y-m', strtotime($arr_date)) > date('Y-m')) {
     开发者_JAVA百科       $total_pages = $page_ctr;
            break;
        }

 }


Try foreach loop like this

$page_ctr = 1;$total_pages=0;
foreach ($arr_date as $k=>$v)
{
  if (date('Y-m', strtotime($v->date)) > date('Y-m')) {
    $total_pages += $page_ctr;
    break;
  }
}


When using objects, you must use object calls. example

$object -> objectcontent

in your case it will be

$arr_date[0]->date

Have a look at this

    //Defining my object to look like yours. I just set the year to 2012 so the while will succeed.
$array = array("date" => "2012-10-20", "page_views" => 2);
    $object = (object) $array;
    $arr_date[] = $object;

//Making an increment variabel
        $i = 0;
        while (true) {

            $page_ctr++;
    //Setting my array $arr_date to iterate through the increment variabel, and output the objects content with the name date
            if (date('Y-m', strtotime($arr_date[$i]->date)) > date('Y-m')) {

                    $total_pages = $page_ctr;

                break;
            }
//Increasing my increment variabel.
            $i++;

     }
     //printing the total pages.
     print $total_pages;

Yet, if i were you, i would use a foreach loop wich iterates better through an array

//Setting increment variable $page_ctr to zero
$page_ctr = 0; 
//Starting foreach array and asking the arrays key value to be set in $key and the value to
//be set in $value. in this case $value will be the object
foreach ($arr_date as $key=>$value) {   
//increasing the increment variable by one
     $page_ctr++;   
//checking your objects date against the current date to see if its bigger.
     if (date('Y-m', strtotime($value->date)) > date('Y-m')) {
       $total_pages = $page_ctr;
//Breaking
       break;
     }

}

This is a copy-paste ready code for testing and seeing for yourself.

<?php
$array = array("date" => "2012-10-20", "page_views" => 2);
$object = (object) $array;
$arr_date[] = $object;

$page_ctr = 0; 
$total_pages = 0;

foreach ($arr_date as $key=>$value) {   

     $page_ctr++;   

     if (date('Y-m', strtotime($value->date)) > date('Y-m')) {
       $total_pages = $page_ctr;

       break;
     }

}

 print $total_pages;


?>


I'm not entirely sure what your actual overall problem is that you try to solve, but you could encapsulate your date selecting problem into an array filter and then just pick what you need. Take care you don't create an endless loop, in any case:

class ArrayCallbackFilter extends FilterIterator
{
    private $param;
    private $callback;
    public function __construct($param, $callback, Array $array)
    {
        $this->param = $param;
        $this->callback = $callback;
        $iterator = new ArrayIterator($array);
        parent::__construct($iterator);
    }
    public function accept()
    {
        $current = $this->getInnerIterator()->current();
        $func = $this->callback;
        return (bool) $func($this->param, $current);
    }
}

$dateIsLarger = function($date, $object)
{
    return (date('Y-m', strtotime($object->date)) > $date);
};


$date = date('Y-m');
$total_pages = FALSE;
$page_ctr = 0;
while (true) {

        $page_ctr++;

        foreach(new ArrayCallbackFilter($date, $dateIsLarger, $dates) as $object)
        {
            $total_pages = $page_ctr;
            break 2; # <<-- break the right level
        }
        throw new Exception('Endless Loop.');
}

Demo/Sandbox to play around for yourself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜