Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP第n个foreach记录_Php - Fatal编程技术网

PHP第n个foreach记录

PHP第n个foreach记录,php,Php,我正在寻找一种方法来执行稍微复杂的第n个记录调整,类似于PHP中jQuery的/CSS3nth child(3n-1),因此每行有三个列,中间的一个需要添加类“mid”。我最好不要使用jQuery,因为页面加载和所添加的类似乎总是存在延迟 我知道这意味着$zxc%2,但我们都从某个地方开始 <?php $zxc = 1; ?> <?php foreach($products as $product): ?> <div class="theProductListIte

我正在寻找一种方法来执行稍微复杂的第n个记录调整,类似于PHP中jQuery的/CSS3
nth child(3n-1)
,因此每行有三个列,中间的一个需要添加类“mid”。我最好不要使用jQuery,因为页面加载和所添加的类似乎总是存在延迟

我知道这意味着
$zxc%2
,但我们都从某个地方开始

<?php $zxc = 1; ?>
<?php foreach($products as $product): ?>
<div class="theProductListItem<?php if($zxc % (3-1) == 0){?> mid<?php } ?>">
<!--product contents-->
</div>
<?php $zxc++; ?>
<?php endforeach; ?>

使用以下命令:

if( ($zxc % 3) == 1)

您需要使用
%3
,因为根据定义,您需要“每三项”

然后你有一些选择,你可以从一个偏移开始你的变量。 e、 g

您也可以从更常见的0或1开始,并更改比较

$x = 0;
foreach ($array as $item) {
    ...
    if ($x % 3 == 1) {
        ...
    }
    ...
}
表面上,您可以将最后一个示例更改为此

$x = 0;
foreach ($array as $item) {
    ...
    if (($x % 3) - 1 == 0) {
        ...
    }
    ...
}

这是一种实现您想要的功能的方法,而不必担心模数计算,尽管这是理所当然的,但它有点冗长。它通过一些跳过记录的方法扩展了标准:

class NthArrayItemIterator extends ArrayIterator
{
    private $step;
    private $offset;

    public function __construct($array, $step = 1, $offset = 0)
    {
        parent::__construct($array);
        $this->step = $step;
        $this->offset = $offset;
    }

    private function skipn($n)
    {
        echo "skipn($n)\n";
        while ($n-- && $this->valid()) {
            parent::next();
        }
    }

    public function rewind()
    {
        parent::rewind();
        $this->skipn($this->offset);
    }

    public function next()
    {
        $this->skipn($this->step);
    }
}

foreach (new NthArrayItemIterator(array(1, 2, 3, 4, 5, 6), 3, 2) as $item) {
    echo "$item<br />";
}
类NthArrayItemIterator扩展了arrayiiterator
{
私人$step;
私人美元抵销;
公共函数构造($array,$step=1,$offset=0)
{
父项::_构造($array);
$this->step=$step;
$this->offset=$offset;
}
专用函数skipn($n)
{
回显“skipn($n)\n”;
而($n--&&$this->valid()){
父::下一步();
}
}
公共函数倒带()
{
父::倒带();
$this->skipn($this->offset);
}
公共职能下一步()
{
$this->skipn($this->step);
}
}
foreach(新的NthArrayItemIterator(数组(1,2,3,4,5,6),3,2)作为$item){
回显“$item
”; }


在这种情况下,它输出第三个和第六个项目。

为什么不只是如果($i==3){$i=1;}那么您可以始终检查$i是否为3或其他,它将始终是第三个记录。为什么不使用CSS3
nth-child()
?您好。我不想要第三条记录,我正在寻找第三条记录,或第五条记录,或任何类似的记录。@spudley-“我最好不要使用jQuery,因为页面加载和添加的类似乎总是有延迟。”在jQuery和CSS3中,我的大部分工作(跨浏览器位和BOB等)都太不成熟,而且命中率太低@Daniel:是的,这就是为什么每次$i达到3时你都会重置它。他在寻找2、5、8。我不认为你的工作与此。完美。谢谢hjpotter92,如果($zxc%3)==2)做到了。@Steve中间人需要上课
class NthArrayItemIterator extends ArrayIterator
{
    private $step;
    private $offset;

    public function __construct($array, $step = 1, $offset = 0)
    {
        parent::__construct($array);
        $this->step = $step;
        $this->offset = $offset;
    }

    private function skipn($n)
    {
        echo "skipn($n)\n";
        while ($n-- && $this->valid()) {
            parent::next();
        }
    }

    public function rewind()
    {
        parent::rewind();
        $this->skipn($this->offset);
    }

    public function next()
    {
        $this->skipn($this->step);
    }
}

foreach (new NthArrayItemIterator(array(1, 2, 3, 4, 5, 6), 3, 2) as $item) {
    echo "$item<br />";
}