基于 in_array 的 if/then/else 语句

发布于 2024-11-24 10:21:39 字数 1013 浏览 2 评论 0原文

我希望在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠我空喜 2024-12-01 10:21:39

这里的技巧是切换数组,例如

<?php
$my_arr = get_custom_field('othermulti');

$opts = array(
    'Man' => '<div>Man description</div>',
    'Bear' => '<div>Bear description</div>',
    'Pig' => '<div>Pig description</div>',
);

$opts_arr = array_keys($opts);

if ( is_array($my_arr) ) {
    foreach($opts_arr as $opt) {
       if ( in_array($opt, $my_arr) ) {
          print $opts[$opt]; // will print the description for checked items.
       }
       else {
         print $opt . ' was not checked.';
       }
    }
}
else {
    print 'No options checked.';
}
?>

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.

<?php
$my_arr = get_custom_field('othermulti');

$opts = array(
    'Man' => '<div>Man description</div>',
    'Bear' => '<div>Bear description</div>',
    'Pig' => '<div>Pig description</div>',
);

$opts_arr = array_keys($opts);

if ( is_array($my_arr) ) {
    foreach($opts_arr as $opt) {
       if ( in_array($opt, $my_arr) ) {
          print $opts[$opt]; // will print the description for checked items.
       }
       else {
         print $opt . ' was not checked.';
       }
    }
}
else {
    print 'No options checked.';
}
?>

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

茶底世界 2024-12-01 10:21:39

所发生的情况表明实际上 $my_arr 的值都不与 $opts 的值匹配。我想你想使用

if (in_array($my_arr[$k], array_keys($opts)) !== TRUE) {

另请注意 $my_arr[$k] === $vin_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 use

if (in_array($my_arr[$k], array_keys($opts)) !== TRUE) {

Also note that $my_arr[$k] === $v and in_array($x, $y) !== TRUE === !in_array($x, $y)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文