迭代器到数组

发布于 2024-08-04 17:35:48 字数 562 浏览 5 评论 0原文

DatePeriod 是一个用于处理重复日期的 PHP 类。它的方法数量非常有限。因此,当我想使用重复日期执行基本数组函数时,我必须使用 iterator_to_array 将其复制到数组中。奇怪的是,复制它似乎会破坏它。有什么想法吗?

$p=new DatePeriod(date_create('2008-01-01'),
                  DateInterval::createFromDateString( "+2 days" ),
                  date_create('2008-12-31')); 

echo count(iterator_to_array($p)); //183 

$a=iterator_to_array($p); 
echo count($a); //0 

DatePeriod is a PHP class for handling recurring dates. It has a very limited number of methods. So when I want to do basic array functions with the recurring dates, I have to copy it to an array with iterator_to_array. Strangely, copying it seems to clobber it. Any ideas why?

$p=new DatePeriod(date_create('2008-01-01'),
                  DateInterval::createFromDateString( "+2 days" ),
                  date_create('2008-12-31')); 

echo count(iterator_to_array($p)); //183 

$a=iterator_to_array($p); 
echo count($a); //0 

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

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

发布评论

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

评论(3

溇涏 2024-08-11 17:35:48

这就是我要做的。我将扩展 DatePeriod 并实现 toArray 方法:

class MyDatePeriod extends DatePeriod
{
    public $dates;

    public function toArray()
    {
        if ($this->dates === null) {
            $this->dates = iterator_to_array($this);
        }

        return $this->dates;
    }
}

$p = new MyDatePeriod(date_create('2008-01-01'),
                      DateInterval::createFromDateString( "+2 days" ),
                      date_create('2008-12-31'));

echo count($p->toArray()) . "\n"; // 183

echo count($p->toArray()) . "\n"; // 183

Here's what I'd do. I'd extend DatePeriod and implement a toArray method:

class MyDatePeriod extends DatePeriod
{
    public $dates;

    public function toArray()
    {
        if ($this->dates === null) {
            $this->dates = iterator_to_array($this);
        }

        return $this->dates;
    }
}

$p = new MyDatePeriod(date_create('2008-01-01'),
                      DateInterval::createFromDateString( "+2 days" ),
                      date_create('2008-12-31'));

echo count($p->toArray()) . "\n"; // 183

echo count($p->toArray()) . "\n"; // 183
等待圉鍢 2024-08-11 17:35:48

我想知道迭代器是否没有被 iterator_to_array() 重新缠绕,因此第二个调用开始以光标结尾进行迭代。您可以尝试以下操作:

$p->rewind()
$a=iterator_to_array($p); 

如果迭代器不可回滚,您可以尝试在遍历对象之前克隆该对象,例如

$p2 = clone $p;
echo count(iterator_to_array($p2));

$array = iterator_to_array($p);

I wonder if maybe the iterator isn't being re-wound by iterator_to_array(), so the second call starts iterating with the cursor at the end. You could try this:

$p->rewind()
$a=iterator_to_array($p); 

If the iterator is not rewindable, you could try cloning the object before you traverse it, e.g.

$p2 = clone $p;
echo count(iterator_to_array($p2));

$array = iterator_to_array($p);
青巷忧颜 2024-08-11 17:35:48

据推测,第一次调用会遍历迭代器中的所有元素(即调用 next() 直到 valid() 为 false)。明智的行为是 iterator_to_array 从迭代器中的当前位置开始转换 - 让其静默倒回将是不灵活的,并且可能会引发错误。

在再次使用迭代器之前尝试倒回迭代器。

$p=new DatePeriod(date_create('2008-01-01'),
                  DateInterval::createFromDateString( "+2 days" ),
                  date_create('2008-12-31')); 

echo count(iterator_to_array($p)); //183 

$p->rewind(); // Newly added!

$a=iterator_to_array($p); 
echo count($a); //0

Presumably, the first call traverses all the elements in the iterator (i.e. calls next() until valid() is false). The sensible behaviour is for iterator_to_array to begin the conversion from the current position in the iterator - having it silently rewind would be inflexible, and possibly bug inducing.

Try rewinding the iterator before using it again.

$p=new DatePeriod(date_create('2008-01-01'),
                  DateInterval::createFromDateString( "+2 days" ),
                  date_create('2008-12-31')); 

echo count(iterator_to_array($p)); //183 

$p->rewind(); // Newly added!

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