-1

php foreach如何分组?我已经尝试了5天,但仍然无法使用我不知道php foreach它是如何工作的,但我正在学习它,谢谢大家的建议

原始的PHP:

<?php if(isset($this->leading) && count($this->leading)): ?>
        <?php foreach($this->leading as $key=>$item): ?>
            <?php

                $this->item=$item;
                echo $this->loadTemplate('item');
            ?>
        <?php endforeach; ?>
    <?php endif; ?>


    <?php if(isset($this->primary) && count($this->primary)): ?>
        <?php foreach($this->primary as $key=>$item): ?>
            <?php

                $this->item=$item;
                echo $this->loadTemplate('item');
            ?>
        <?php endforeach; ?>
    <?php endif; ?>


    <?php if(isset($this->secondary) && count($this->secondary)): ?>
        <?php foreach($this->secondary as $key=>$item): ?>          
            <?php

                $this->item=$item;
                echo $this->loadTemplate('item');
            ?>
        <?php endforeach; ?>
    <?php endif; ?>

我试过了

<?php if(isset($this->leading) && count($this->leading)) && (isset($this->primary) && count($this->primary)) && (isset($this->secondary) && count($this->secondary)): ?>
    <!-- Leading items -->
        <?php foreach (array($this->leading, $this->primary, $this->secondary) as $key=>$item)          ($this->leading as $key=>$item): ?>
            <?php
                // Load category_item.php by default
                $this->item=$item;
                echo $this->loadTemplate('item');
            ?>
        <?php endforeach; ?>
    <?php endif; ?>

但不工作

一如既往,感谢您的帮助!

感谢大家:)

谢谢,史蒂文!

4

3 回答 3

1

你的意思是这样的..?

<?php

$arr = array();
$arr[] = $this->leading;
$arr[] = $this->primary;
$arr[] = $this->secondary;

foreach($arr as $v) {    
    if(is_array($v) && (count($v) > 0)) {
        foreach($v as $item) {
            $this->item=$item;
            echo $this->loadTemplate('item');
        }
    }
}

?>
于 2013-07-08T09:54:19.480 回答
0

定义一个函数,如:

function foobar ($tmp) {
    if(isset($tmp) && count($tmp)) { 
        foreach ($tmp as $key => $item) {
            // Load category_item.php by default
            $this->item = $item;
            echo $this->loadTemplate('item');
        }
    }   
}

并用您的数据调用它:

foobar($this->leading);
foobar($this->primary);
foobar($this->secondary);
于 2013-07-08T09:55:30.933 回答
0

处理此问题的最简单方法是首先构建一个包含您需要的所有项目的列表,然后仅使用一个 foreach 循环浏览该列表。

我还建议在是否定义三个变量(前导、主要、次要)方面保持一致。如果您可以保证它们已设置并且是数组 - 即使它们是空数组,它也可以使您的代码像这样简单:

<?php
$items = array_merge($this->leading, $this->primary, $this->secondary);
foreach ($items as $Item) {
    $this->item = $item;
    $this->loadTemplate('item');
}
?>

如果您真的不知道变量是否已设置,并且您无法在其他地方重构代码来更改它,您可以这样做:

<?php
$items = array();
if (isset($this->leading)) $items = array_merge($items, $this->leading);
if (isset($this->primary)) $items = array_merge($items, $this->primary);
if (isset($this->secondary)) $items = array_merge($items, $this->secondary);
foreach ($items as $Item) {
    $this->item = $item;
    $this->loadTemplate('item');
}
?>
于 2013-07-08T18:12:47.803 回答