Php 带有变量函数的数组中的Foreach变量列表返回未定义的偏移量:Error
更新: 如果我从第67行和第68行中删除Php 带有变量函数的数组中的Foreach变量列表返回未定义的偏移量:Error,php,function,undefined,Php,Function,Undefined,更新: 如果我从第67行和第68行中删除$1,则所有未定义的偏移量2错误都将消失 只有一个未定义的偏移量1错误,即没有x的var返回为“自定义” $格式的结果 print_r($formats); Array ( [data] => Array ( [all] => Array ( [horizontal] => Array
$1
,则所有未定义的偏移量2错误都将消失
只有一个未定义的偏移量1错误,即没有x的var返回为“自定义”
$格式的结果
print_r($formats);
Array
(
[data] => Array
(
[all] => Array
(
[horizontal] => Array
(
[0] => 728x90
[1] => 468x60
)
[vertical] => Array
(
[0] => 160x600
[1] => 120x600
)
[square] => Array
(
[0] => 300x250
[1] => 125x125
)
[custom] => Array
(
[0] => custom
)
)
)
[types] => Array
(
[text] => Text ads
[image] => Image ads
[ref_text] => Text referrals
[ref_image] => Image referrals
[textimage] => Text and image ads
[link] => Ad links
[video] => Video ads
[all] => All ad types
)
[sections] => Array
(
[horizontal] => Horizontal
[vertical] => Vertical
[square] => Square
[other] => Other ad formats
[custom] => Custom width and height
)
[formats] => Array
(
[728x90] => %1$s x %2$s Leaderboard
[468x60] => %1$s x %2$s Banner
[160x600] => %1$s x %2$s Wide Skyscraper
[120x600] => %1$s x %2$s Skyscraper
[300x250] => %1$s x %2$s Medium Rectangle
[125x125] => %1$s x %2$s Button
[custom] => Custom width and height
)
)
错误在第67行,这是代码
<?php foreach ($sformats as $sformat) : ?>
<?php list($w, $h, $l) = OX_Tools::explode_format($sformat); //Line 67?>
<option<?php echo ($adformat == $sformat ? ' selected="selected"' : ''); ?> value="<?php echo $sformat; ?>"> <?php printf($formats['formats'][$sformat], $w, $h, $l); ?></option>
<?php endforeach; ?>
这就是我想出的解决办法。对不起,有些人不喜欢这个问题。也许是因为我没有提供一个可执行的例子,或者过去有人问过类似的问题,没有一个和我的问题完全一样 我已经在Stack上找到了很多解决问题的方法,并且意识到我没有回馈,因此我计划花更多的时间在这个网站上,并将努力做出贡献 够了!当我在等待是否有人会说你错过了这个
东西时,我正在查看列表中的$1
变量,并试图找出原因!这个项目中有38个文件,不包括javascript和语言文件,它一定是为了从以前的作者那里删除的东西而存在的
当我从第67行和第68行的列表($w,$h,$l)
中删除$1
时,除了一个错误之外,其他所有错误都消失了,代码按预期执行
正如我在更新中所述,这只给我留下了一个未定义的偏移量错误,并且必须与变量相关,该变量与preg_split()
函数可能匹配的变量不匹配
我尝试的是如果$sformat!='自定义“
它将使用preg_split()
并清除所有错误。我测试了插件的其余部分,没有错误。伟大的因为用户可以创建自己的命名格式,所以任何他们想要匹配的custom
都是不好的。我使用了preg_match()
函数来消除任何不一致的变量
这是原始的第67行
<?php list($w, $h, $l) = OX_Tools::explode_format($sformat); ?>
我把它改成了
<?php if (!preg_match("/[0-9]x[0-9]/", $sformat)) {$sformat = $sformat;}else{list($w, $h) = OX_Tools::explode_format($sformat); } ?>
如果您发现我的解决方案存在问题,请发表评论
感谢@Marcos pérez Gude的评论,但是如果它是explode(“x”,$var)
那么简单的话,我就不会问人们不喜欢的问题,也不会激发人们更多地参与Stackoverflow社区
过好每一天,就像这是你的最后一天…。总有一天会的 这个密码对我来说毫无意义。你需要附上一个清晰的可验证的例子来重现这个问题。例如,您没有附加与$formats['formats'][$sformat]
相关的代码,我认为未定义的偏移量错误就在这里……因此我在return$vars[0]中添加了索引代码>在函数中,错误消失了,但是html的输出不正确这是我的问题所在,有人不喜欢我,我不确定这是我需要编辑的函数还是第67行。html结果7 x 2排行榜
4 x 6横幅
应该是728 x 90排行榜
468 x 60横幅
将查看第68行,看看我是否可以使html输出正确,或者返回并继续查看67为什么需要预拆分?explode(“x”,$var)
?并非所有var都有x在本例中,一个var是“自定义”的,我认为最初的作者这样做是为了将$w
和$h
分开,以显示在选项printf($formats['formats'[$sformat],$w,$h)中
我将$formats
的结果添加到原始帖子中,这样就可以查看代码示例中使用的所有数据@马尔科斯佩雷兹古德
<?php
static function explode_format($format)
{
$vars = preg_split("/[x#]+/", $format);
return $vars;
}
?>
<?php list($w, $h, $l) = OX_Tools::explode_format($sformat); ?>
<?php if (!preg_match("/[0-9]x[0-9]/", $sformat)) {$sformat = $sformat;}else{list($w, $h) = OX_Tools::explode_format($sformat); } ?>