【发布时间】:2013-09-09 20:35:35
【问题描述】:
我希望在 in_array 语句中创建一个条件。基本上,我希望为 Wordpress 输出的数组中的每个键返回一个值(在 div 标签内)。本质上,Wordpress 根据后台管理后台的复选框对话框从这个数组中输出键。因此,如果未找到数组中的某个键(因为管理员没有在后端的复选框中单击它),那么它将根本不会显示它。
这是我能确定的最接近的代码。我决定出于测试目的,如果数组中的键不存在,我将暂时返回“Nope”字样(而不是上面段落中提到的“根本不显示它”)。
$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';
}
?>
上面的代码已经过测试,它确实为所有内容显示“选项__描述”。 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).这是不正确的,我希望得到它(在这种情况下是为了便于测试)上述语句的“else”部分中的回声。
更新 2:当前代码在这里:http://codepad.org/nxzFUMMn
更新:当前代码在这里:http://codepad.org/iXVbmLGL
【问题讨论】:
-
$my_arr长什么样子? -
@tandu 我在 $my_arr 上做了一个 var_export。另外,请注意原始帖子中的更新 2,因为代码已更改。我快到了! var_export 输出:数组 ( 0 => '选项 1', 1 => '选项 3', )
标签: php arrays if-statement