Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.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 函数返回未定义的偏移量错误_Php_Arrays_Function_Undefined_Offset - Fatal编程技术网

Php 函数返回未定义的偏移量错误

Php 函数返回未定义的偏移量错误,php,arrays,function,undefined,offset,Php,Arrays,Function,Undefined,Offset,我正在尝试解决未定义的偏移量。我在谷歌上搜索过堆栈溢出,但我发现的例子要么不适用,要么太复杂,我的技术人员此时无法理解。我是个新手,但在寻求帮助和智慧之前,我已经尽了我所承诺的努力 这就是现在存在的功能: function PrintFolio($aaPh) //aaPh =associative array place holder { //print out X number rows of unto 4 images from array with X items //if array

我正在尝试解决未定义的偏移量。我在谷歌上搜索过堆栈溢出,但我发现的例子要么不适用,要么太复杂,我的技术人员此时无法理解。我是个新手,但在寻求帮助和智慧之前,我已经尽了我所承诺的努力

这就是现在存在的功能:

function PrintFolio($aaPh) //aaPh =associative array place holder

{
//print out X number rows of unto 4 images from array with X items
//if array had 7 items, print one row of 4 next row 3
//if array had 16 items, print 4 rows of 4, etc.
    //if array had 13 items, print 3 rows of 4, final row with one item

$itemsCount = sizeof($aaPh);//get size of array

$height = (int)ceil(sizeof($aaPh)/4);//rounded up division by 4

//$height = $height + 1;
$keys = array_keys($aaPh); //get keys

//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
        {
             $aaPhIndex = array_keys($aaPh)[$row*4+$image]; //if associative array
             if( $row*4+$image < $itemsCount ) {
                 $aaPhIndex = $keys[$row*4+$image];
                 printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]);
                 //$template = '<div class="3u"><a href="_img/fulls/%1$s" class="image full"><img src="_img/thumbs/%1$s" alt="" title="%2$s" /></a></div>';
            }
        }

        echo '</div>'; //end div group of 4
    }//end loop
}//end function
函数PrintFolio($aaPh)//aaPh=关联数组占位符
{
//从包含X个项目的数组中打印出X行至4个图像
//如果数组有7项,则打印一行4项,下一行3项
//如果数组有16项,则打印4行,共4行,以此类推。
//如果数组有13项,则打印3行,共4行,最后一行有一项
$itemscont=sizeof($aaPh);//获取数组的大小
$height=(int)ceil(sizeof($aaPh)/4);//四舍五入除以4
//$height=$height+1;
$keys=array_keys($aaPh);//获取密钥
//循环遍历X项数组,每组4项作为一行打印
对于($row=0;$row<$height;$row++)//在行中循环
{
回显“”;//打开div
对于($image=0;$image<4;$image++)//每行由4个图像组成
{
$aaPhIndex=array_keys($aaPh)[$row*4+$image];//如果是关联数组
如果($row*4+$image<$itemscont){
$aaPhIndex=$keys[$row*4+$image];
printf(模板组合,$aaPhIndex,$aaPh[$aaPhIndex]);
//$template='';
}
}
echo“”;//结束div组,共4个
}//端环
}//端函数
它获取一个数组,将其切成四个单位,然后将数组作为一系列图像打印到屏幕上。但是,如果我没有一个可以精确设计为4的数字,它将显示任何未定义的偏移量错误(我希望我说的是正确的)

  • 我的目标是在不修改我的站点错误报告功能的情况下,不让未定义的偏移量错误打印到这些屏幕上(我认为这样做是欺骗,因为它不会纠正问题,只是隐藏它,这在我看来并不太光明正大)

因为你不能假设你总是有很多元素可以被四整除,所以你需要测试每一个元素。在计算索引然后使用它引用数组的内部循环中,只需添加一个检查以确保数组元素存在。由此:

$aaPhIndex = $keys[$row*4+$image];
printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]);
为此:

$aaPhIndex = $keys[$row*4+$image];
if (isset($aaPh[$aaPhIndex])) {
    printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]);
}

因为你不能假设你总是有很多元素可以被四整除,你需要测试每一个元素。在计算索引然后使用它引用数组的内部循环中,只需添加一个检查以确保数组元素存在。由此:

$aaPhIndex = $keys[$row*4+$image];
printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]);
为此:

$aaPhIndex = $keys[$row*4+$image];
if (isset($aaPh[$aaPhIndex])) {
    printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]);
}

因为你不能假设你总是有很多元素可以被四整除,你需要测试每一个元素。在计算索引然后使用它引用数组的内部循环中,只需添加一个检查以确保数组元素存在。由此:

$aaPhIndex = $keys[$row*4+$image];
printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]);
为此:

$aaPhIndex = $keys[$row*4+$image];
if (isset($aaPh[$aaPhIndex])) {
    printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]);
}

因为你不能假设你总是有很多元素可以被四整除,你需要测试每一个元素。在计算索引然后使用它引用数组的内部循环中,只需添加一个检查以确保数组元素存在。由此:

$aaPhIndex = $keys[$row*4+$image];
printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]);
为此:

$aaPhIndex = $keys[$row*4+$image];
if (isset($aaPh[$aaPhIndex])) {
    printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]);
}

为什么不在每个第四个元素后面加一个换行符呢

function PrintFolio($aaPh) //aaPh =associative array place holder
{
//loop through the array
for($row = 0; $row < Count($aaPh); $row++)    //looping through the rows
{
    if (!$row || !($row%4)) {
        if ($row) echo '</div>'; // close previously opened divs
        echo '<div class="row flush">'; //open div
    }
    /* show your picture code here */

}//end loop
echo '</div>';
}//end function
函数PrintFolio($aaPh)//aaPh=关联数组占位符
{
//在数组中循环
for($row=0;$row
以下是您评论后的更新。 由于您不需要关联数组的值,只需要键,因此可以按如下方式更改代码:

function PrintFolio($aaPh) //aaPh =associative array place holder
{
//loop through the array
$row=0;
foreach(array_keys($aaPh) as $img)    //looping through array keys only
{
    if (!$row || !($row%4)) {
        if ($row) echo '</div>'; // close previously opened divs
        echo '<div class="row flush">'; //open div
    }
    echo '<img src="' . $img . '">';
    $row++; // increase row counter.
}//end loop
echo '</div>';
}//end function
函数PrintFolio($aaPh)//aaPh=关联数组占位符
{
//在数组中循环
$row=0;
foreach(数组_键($aaPh)作为$img)//仅在数组键之间循环
{
如果(!$row | |!($row%4)){
if($row)回显“”;//关闭以前打开的div
回显“”;//打开div
}
回声';
$row++;//增加行计数器。
}//端环
回声';
}//端函数

确保将正确的路径放入标记中

为什么不在每个第四个元素后面加一个换行符呢

function PrintFolio($aaPh) //aaPh =associative array place holder
{
//loop through the array
for($row = 0; $row < Count($aaPh); $row++)    //looping through the rows
{
    if (!$row || !($row%4)) {
        if ($row) echo '</div>'; // close previously opened divs
        echo '<div class="row flush">'; //open div
    }
    /* show your picture code here */

}//end loop
echo '</div>';
}//end function
函数PrintFolio($aaPh)//aaPh=关联数组占位符
{
//在数组中循环
for($row=0;$row
以下是您评论后的更新。 由于您不需要关联数组的值,只需要键,因此可以按如下方式更改代码:

function PrintFolio($aaPh) //aaPh =associative array place holder
{
//loop through the array
$row=0;
foreach(array_keys($aaPh) as $img)    //looping through array keys only
{
    if (!$row || !($row%4)) {
        if ($row) echo '</div>'; // close previously opened divs
        echo '<div class="row flush">'; //open div
    }
    echo '<img src="' . $img . '">';
    $row++; // increase row counter.
}//end loop
echo '</div>';
}//end function
函数PrintFolio($aaPh)//aaPh=关联数组占位符
{
//在数组中循环
$row=0;
foreach(数组_键($aaPh)作为$img)//仅在数组键之间循环
{
如果(!$row | |!($row%4)){
if($row)回显“”;//关闭以前打开的div
回显“”;//打开div
}
回声';
$row++;//增加行计数器。
}//端环
回声';
}//端函数

确保将正确的路径放入标记中

为什么不在每个第四个元素后面加一个换行符呢

function PrintFolio($aaPh) //aaPh =associative array place holder
{
//loop through the array
for($row = 0; $row < Count($aaPh); $row++)    //looping through the rows
{
    if (!$row || !($row%4)) {
        if ($row) echo '</div>'; // close previously opened divs
        echo '<div class="row flush">'; //open div
    }
    /* show your picture code here */

}//end loop
echo '</div>';
}//end function
函数PrintFolio($aaPh)//aaPh=关联数组占位符
{
//在数组中循环
for($row=0;$row
以下是您评论后的更新。 因为您不需要关联数组的值,所以