我想要的是......
我不知道check_mobile_exists( $mobiles, $electronics )
这个函数一直都是真的。我想在$ electronics键列表中存在$ mobile数组键时为true,如果不存在则为false。在此结果的基础上,我循环发布结果。但正如我所说,函数在每次迭代时都返回true。任何人建议我现在应该做什么?
function check_mobile_exists( $mobiles, $electronics ) {
foreach( $mobiles as $mobile => $price ) {
if( array_key_exists( $mobile, $electronics ) ) {
return true;
}
return false;
}
}
$mobiles = array(
'iPhone' => '$350',
'tablet' => '$100',
'samsung' => '$200',
'walton' => '$60'
);
$electronics = array(
'iPhone' => 'OK',
'tablet' => 'will do',
'walton' => 'No need'
);
while( have_posts() ): the_post();
if( check_mobile_exists( $mobiles, $electronics ) ){ // returning only true on every iterating
echo get_the_title() .' do something....<br />';
} else {
echo 'Do another....';
}
endwhile;
答案 0 :(得分:0)
试试这个
$mobiles = array(
'iPhone' => '$350',
'tablet' => '$100',
'samsung' => '$200',
'walton' => '$60'
);
$electronics = array(
'iPhone' => 'OK',
'tablet' => 'will do',
'walton' => 'No need'
);
while( have_posts() ): the_post();
foreach( $mobiles as $mobile => $price ) {
if( array_key_exists( $mobile, $electronics ) ) {
echo get_the_title() .' do something....<br />';
}else{
echo 'Do another....';
}
}
endwhile;
更新:正如您所说,不使用其他功能,您可以这样做。
答案 1 :(得分:0)
无需检查移动功能
$mobiles = array(
'iPhone' => '$350',
'tablet' => '$100',
'samsung' => '$200',
'walton' => '$60'
);
$electronics = array(
'iPhone' => 'OK',
'tablet' => 'will do',
'walton' => 'No need'
);
while( have_posts() ): the_post();
foreach ( $mobiles as $m_key => $mobile ) {
if ( array_key_exists( $m_mey, $electronics) ) {
do somethin
} else {
do somethin
}
}
endwhile;