这是$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
)
)
如何将我的数组$arr_date
放在while循环中? 这还不行。
while (true) {
$page_ctr++;
if (date('Y-m', strtotime($arr_date)) > date('Y-m')) {
$total_pages = $page_ctr;
break;
}
}
答案 0 :(得分:0)
尝试这样的foreach循环
$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;
}
}
答案 1 :(得分:0)
使用对象时,必须使用对象调用。 示例
$object -> objectcontent
在你的情况下它将是
$arr_date[0]->date
看看这个
//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;
然而,如果我是你,我会使用一个foreach循环,它通过数组更好地迭代
//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;
}
}
这是一个用于测试和查看的复制粘贴就绪代码。
<?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;
?>
答案 2 :(得分:0)
我不完全确定您尝试解决的实际整体问题是什么,但您可以将日期选择问题封装到数组过滤器中,然后选择您需要的内容。在任何情况下,请注意不要创建无限循环:
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为自己玩。