为什么foreach语句中的if语句不起作用?我的array_search函数也无法正常工作,即使用yii framework
if语句应打印品牌名称,但它打印为false,我可能知道if语句为什么返回空值或空的情况,提前谢谢
function getColumnKey($brand_name){
$columnKey = '';
$five_up_brands = array('K'=>"Coke",'L'=>"Sprite",'M'=>"Royal");
array_search($brand_name, $five_up_brands);
foreach ($five_up_brands as $k => $v) {
if($v == $brand_name){
$columnKey = $k;
}
}
return $columnKey;
}
答案 0 :(得分:1)
我认为您希望在匹配品牌名称时立即返回,而不是让条件不再适用时循环进入下一次迭代。
function getColumnKey($brand_name){
$columnKey = '';
$five_up_brands = array( 'K'=>"Coke", 'L'=>"Sprite", 'M'=>"Royal" );
/* what is the point of this - it is not used? */
#array_search( $brand_name, $five_up_brands );
foreach( $five_up_brands as $k => $v ) {
if( $v == $brand_name ){
return $k;
}
}
return false;
}
答案 1 :(得分:0)
我刚试过这段代码..
工作正常。它给了我这样的输出
string(1)" L"
<?php
function getColumnKey($brand_name){
$five_up_brands = array('K'=>"Coke",'L'=>"Sprite",'M'=>"Royal");
return array_search($brand_name, $five_up_brands);
}
var_dump(getColumnKey('Sprite'));
答案 2 :(得分:0)
数组搜索可以正常工作
$five_up_brands = array('K'=>"Coke",'L'=>"Sprite",'M'=>"Royal");
$b = array_search("Coke",$five_up_brands);
echo "$b";
这对我有用,请查看。
希望这有帮助
答案 3 :(得分:-2)
试试这个......工作代码:
<g:checkBox name="Polish" value="" checked="false" />
<g:if test="checkbox named polish == checked">
<g:link action="createPolishPDF" id="${oh.id}">${oh.id}</g:link>
</g:if>
<g:else>
<g:link action="createPDF" id="${oh.id}">${oh.id}</g:link>
</g:else>