将特定值的if语句添加到php数组

时间:2018-03-08 15:11:11

标签: php arrays if-statement

我从WordPress Stack Exchange中发布的问题得到以下答案,但如何为特定值添加elseif(if-statement)?例如,他们可能输入“£0”,在这种情况下我不想显示任何内容。

$giving_back = array();

$givingback_details_funds = get_post_meta( $post->ID,'givingback_details_funds', true );

if ( $givingback_details_funds ) {
$giving_back[] = $givingback_details_funds;
}

$givingback_details_days = get_post_meta( $post->ID, 'givingback_details_days', true );

if ( $givingback_details_days ) {
$giving_back[] = _n( 'one working day', $givingback_details_days. ' working days', $givingback_details_days );
}

if ( ! empty( $giving_back ) ) {
echo 'the team donated' . implode( ' and ', $giving_back );
}

1 个答案:

答案 0 :(得分:0)

根据您的使用情况,您可能需要使用strpos或preg_match来搜索有问题的字符串:

if (strpos($givingback_details_funds, "£0") !== false){
    //do something where "£0" is in string
}

preg_match情况是指£和0之间的空间。

if (preg_match("/^£\s*0$/". $givingback_details_funds)){
    //do something where "£0" is in string
}