【问题标题】:PHP Function 'overloops' array returns multiple instances of same objectPHP函数'overloops'数组返回同一对象的多个实例
【发布时间】:2014-02-10 15:40:32
【问题描述】:

这里是新手,正在开发他们的第二个功能。我在谷歌上搜索了一下我的问题,查看了 PHP.net 和一些关于堆栈溢出、WC3schools、CoralCode 等的文章,但我还没有找到任何我能理解的东西(我已经完成了我的我保证在乞讨之前尽职尽责。

我的函数的概念是,它将获取一个任意大小的数组,并以四行打印出来。函数当前返回的重复本身总体等于数组的大小。因此,如果数组有 8 个项目,它将返回九个组(通过在它们之间的白线视觉定义的分组)九个项目。第一个和最后一个有 16 个盒子,2-8 个有 32 个盒子,我希望得到两排四排,两排之间有一条线

here for visual errorhere for how it's meant to look

总而言之,我的循环过度迭代/返回太多/没有以我希望的方式返回数据。我不知道如何表达这个问题,但我已尽我所能尽可能地提出这个问题。

function PrintFolio($aaPlaceholder)
    {
    //print out two rows of four from array with 8 items
    //if array had 7 items, then print row of 4 next row 3
    //if array had 16 items, print 4 rows of 4, etc.

    $height = sizeof($aaPlaceholder);//get size of array

    //loop through array of X items, for each group of 4 print as a row
    for($row = 0; $row < $height; $row++)    //looping through the rows

        {
            echo '<div class="row flush">'; //open div
            for($image = 0; $image < 4; $image++)    //each row is composed of 4 images
            {
                foreach($aaPlaceholder as $key => $value) {
                    printf(TEMPLATE_PORTFOLIO, $key, $value);
                    //replaced $template with constant TEMPLATE_PORTFOLIO!
                }
            }

            echo '</div>'; //end div group of 4
        }//end loop
    }//end function

如果答案很明显,请记住我是新手,对我来说并不明显,或者老实说我不会问。

感谢您的帮助或指导 干杯

【问题讨论】:

  • 您的内部foreach 将打印$aaPlaceholder 中的每一项。这将在内部for 循环的每次迭代中发生,而这又会在外部for 循环的每次迭代中发生。所以你输出整个$aaPlaceholder 的次数太多了。
  • 在内部foreach中,再次使用$aaPlaceholder;也许你想要 $aaPlaceholder[$row]。
  • 在两个 for 循环之间,您已经在遍历数组中的所有项目。不应该有其他循环结构。当然,还有更优雅的解决方案,只需一个循环,但从这段代码开始,您应该首先意识到内部的 foreach 不会做任何您想做的事情。
  • 我知道这很重要,我正在重新利用一些先前的代码来尝试让它做一些“更多”的事情。我将尝试添加“行”以查看会发生什么,我现在处于未知领域。谢谢各位。
  • 我将 foreach 行修改为 'foreach($aaPlaceholder[$row] as $key => $value) {' 结果是两个错误。 1.) 未定义的偏移量:0。 2.) 为 foreach() 提供的参数无效。添加 '[$row]' 听起来是个好主意,但我认为我是 foobared。

标签: php arrays function loops


【解决方案1】:

首先,高度不是数组的大小,而是数组的四分之一。 其次,正如 cmets 中已经说明的那样,不应更改内部 foreach:应将其删除。你已经在循环你的元素了,不需要再做一次了。

function PrintFolio($aaPlaceholder)
{
//print out two rows of four from array with 8 items
//if array had 7 items, then print row of 4 next row 3
//if array had 16 items, print 4 rows of 4, etc.

$itemsCount = sizeof($aaPlaceholder);//get size of array 
$height = (int)ceil(sizeof($aaPlaceholder)/4);//get size of array divided by 4

//loop through array of X items, for each group of 4 print as a row
for($row = 0; $row < $height; $row++)    //looping through the rows

    {
        echo '<div class="row flush">'; //open div
        for($image = 0; $image < 4; $image++)    //each row is composed of 4 images
        {
             $aaPlaceholderIndex = $row*4+$image; //the index in the original array
             if( $aaPlaceholderIndex < $itemsCount ) {
                 printf(TEMPLATE_PORTFOLIO, $aaPlaceholderIndex, $aaPlaceholder[$aaPlaceholderIndex]);
            }
        }

        echo '</div>'; //end div group of 4
    }//end loop
}//end function

编辑:如果您的数组具有从 0 到 n 的纯数字键,则此方法有效。否则(例如关联数组)它应该读取 $aaPlaceholderIndex = array_keys($aaPlaceholder)[$row*4+$image];

再次编辑:因此您使用关联数组(键是字符串而不是整数 0..n)。我对此进行了一些编辑。你最好:

  • 定义 $keys = array_keys($aaPlaceholder);在循环之外,您可以在其中定义 $height
  • 定义 $aaPlaceholderIndex = $keys[$row*4+$image];
  • 将这个定义移到“if”语句中,变成

    if( $row*4+$image < $itemsCount )  {
        $aaPlaceholderIndex = $keys[$row*4+$image];
        printf(TEMPLATE_PORTFOLIO, $aaPlaceholderIndex, $aaPlaceholder[$aaPlaceholderIndex]);
    }
    

这样您只运行一次array_keys(有利于性能),并且当$row*4+$image 太大时检索$keys[$row*4+$image] 时避免“未定义偏移”通知。

【讨论】:

  • 谢谢 dirLuca,我会探索你的指导。我正在调用的数组当前读取如下 ($aaGeneric['01.jpg'] = 'Assignment 01';) 这是数组中的第一项,第二次我将 01s 更改为 02s,就像这样通过所有八个数组中的项目。所以我对数组中的键使用字符串值而不是整数(希望我说得对)
  • 谢谢你,dirluca,这是一个非常有用的答案,我非常感谢你的帮助和指导,我将在剩下的时间里玩这个,尽我所能。非常感谢你。超级有用
  • 我对此进行了一些测试,发现我需要将 if 语句从 '
  • 很高兴它有帮助。我对它进行了更多的编辑,我匆匆忙忙地写了一些非关联的东西。我对“
  • 谢谢你,Dirluca,我会再次讨论这个问题,因为我多次返回井中没有问题,因为水(智慧)最好以小而易消化的形式吸收;)谢谢。我将其更改为从“
【解决方案2】:

这可能不是最好的答案,但这是我想出的办法。

我创建了一个名为“limit”的新变量,它等于身高 var 除以 4。然后我注释掉了第二个“for循环”,因为它导致了问题,只留下了第一个for循环和foreach循环。这让我如愿归还了我的数组。

修改后的代码如下:

function PrintFolio($aaPlaceholder)

    {
    //print out two rows of four from array with 8 items
    //if array had 7 items, then print row of 4 next row 3
    //if array had 16 items, print 4 rows of 4, etc.

    $height = sizeof($aaPlaceholder);//get size of array
    $limit = $height % 4;

    //loop through array of X items, for each group of 4 print as a row
    for($row = 0; $row <= $limit; $row++)    //looping through the rows

        {
            echo '<div class="row flush">'; //open div
            //for($image = 0; $image < 4; $image++)    //each row is composed of 4 images
            //{
                foreach($aaPlaceholder as $key => $value) {
                    printf(TEMPLATE_PORTFOLIO, $key, $value);
                    //replaced $template with constant TEMPLATE_PORTFOLIO!
                }
            //}

            echo '</div>'; //end div group of 4
        }//end loop
    }//end function 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多