0

我想要得到的是..

我不知道check_mobile_exists( $mobiles, $electronics )这个函数是否一直返回 true。当 $electronics 密钥列表中存在 $mobile 数组密钥时,我希望为真,如果不存在,则为假。在此结果的基础上,我正在循环发布结果。但正如我所说,该函数在每次迭代时都返回 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;
4

2 回答 2

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;

更新:正如您所说,无需使用其他功能即可执行此操作。

于 2016-12-24T13:13:22.967 回答
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;
于 2016-12-24T13:23:09.130 回答