新手在这里,他们的第二个功能。我在谷歌上搜索了一些问题,看过PHP.net以及关于堆栈溢出,WC3schools,CoralCode等的一些文章,但是我还没有发现任何我能理解的内容(我已经完成了来之前勤奋乞求我保证。)
我的函数的概念是它将采用任何大小的数组,并以四行的形式打印出来。函数当前返回重复本身,总体上等于数组的大小。因此,如果数组有8个项目,它将返回9个项目的九个组(通过在它们之间用白线直观地定义分组)。第一个和最后一个有16个盒子,2-8个盒子里面有32个盒子,我希望能够在两行之间找到两行四行
见here for visual error 见here 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
如果答案很明显,请记住我很新,这对我来说并不明显,或者说我不会问。
感谢您提供任何帮助或指导 欢呼声
答案 0 :(得分:1)
首先,高度不是数组的大小,而是它的四分之一。 其次,正如评论中所述,内部的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)。我对此进行了一些编辑。你最好:
将此定义移到“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]时避免注意“Undefined offset”。 / p>
答案 1 :(得分:0)
这可能不是最好的答案,但这是我想要做的。
我创建了一个名为'limit'的新变量,它等于height 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