基于 in_array 的 if/then/else 语句
我希望在 in_array 语句中创建一个条件。基本上,我希望为 WordPress 输出的数组中的每个键返回一个值(在 div 标签内)。本质上,Wordpress 根据管理后端中的复选框对话框输出该数组中的键。因此,如果在数组中找不到某个键(因为管理员没有在后端的复选框中单击它),那么它根本不会显示它。
这是我可以确定的最接近的代码。我决定,出于测试目的,如果数组中的键不存在,我将暂时返回“不”一词(而不是如上一段中提到的“简单地不显示它”)。
$my_arr = get_custom_field('product_options');
$opts = array(
'Option 1' => '<div>Option 1 description</div>',
'Option 2' => '<div>Option 2 description</div>',
'Option 3' => '<div>Option 3 description</div>',
);
foreach($opts as $k=>$v) {
if (in_array($my_arr[$k],$opts)!==TRUE) echo $v; else echo 'nope';
}
?>
上面的代码已经过测试,它确实显示所有内容的“选项 __ 描述”。当选项实际上并未在数组中输出时,它甚至会显示“选项 2 描述”(基于管理员未在后端单击选项 2)。这是不正确的,我希望将其传递到上述语句的“else”部分中的回显(在本例中是为了便于测试)。
更新 2:当前代码位于:http://codepad.org/nxzFUMMn
< strong>更新:当前代码位于:http://codepad.org/iXVbmLGL
Im looking to create a conditional within an in_array statement. Basically, I would like to have a value (within div tags) returned for every key within an array which is outputted by Wordpress. In essence, Wordpress outputs keys from this array based on a checkbox dialog in the admin backend. So, if a key within the array is not found (because the administrator did not click it on in the checkbox within the backend), than it will simply wont display it at all.
Here is the closest code I can ascertain as being what is needed. I decided that for testing purposes, I would temporarily have the words "Nope" returned if a key within the array is not there (rather than "simply not displaying it" as mentioned in the paragraph above).
$my_arr = get_custom_field('product_options');
$opts = array(
'Option 1' => '<div>Option 1 description</div>',
'Option 2' => '<div>Option 2 description</div>',
'Option 3' => '<div>Option 3 description</div>',
);
foreach($opts as $k=>$v) {
if (in_array($my_arr[$k],$opts)!==TRUE) echo $v; else echo 'nope';
}
?>
The above code has been tested and it does dispaly "Option __ description" for everything. It even displays "Option 2 description" when Option is not actually being outputted within the array (based off of the admin not clicking Option 2 within the backend). This is not correct and I wish to get it to (in this case for ease of testing) the echo within the "else" part of the above statement.
Update 2: Current code is here: http://codepad.org/nxzFUMMn
Update: Current code is here: http://codepad.org/iXVbmLGL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的技巧是切换数组,例如
get_custom_field() 是使用它的插件的自定义模板函数。详细信息请参见以下链接:
http://wordpress.org/support/topic/ifelse -statement-for-custom-checkbox?replies=16
The trick here was to switch the arrays around, e.g.
get_custom_field() is a custom template function for the plugin where this was used. See the following link for the details:
http://wordpress.org/support/topic/ifelse-statement-for-custom-checkbox?replies=16
所发生的情况表明实际上
$my_arr
的值都不与$opts
的值匹配。我想你想使用另请注意
$my_arr[$k]
===$v
和in_array($x, $y) !== TRUE
===!in_array($x, $y)
What is happening indicates that in fact none of the values of
$my_arr
match a value of$opts
. I think you want to useAlso note that
$my_arr[$k]
===$v
andin_array($x, $y) !== TRUE
===!in_array($x, $y)