在 php 中的 array_key_exists() 的每次迭代中返回 true
Returning true on every iteration of array_key_exists() in php
我想得到的是..
我不知道 check_mobile_exists( $mobiles, $electronics )
这个函数一直返回 true。当 $electronics 键列表中存在 $mobile 数组键时我想要 true,当不存在时我想要 false。在此结果基础上,我正在循环 post 结果。但正如我所说,该函数在每次迭代时都返回 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' => '0',
'tablet' => '0',
'samsung' => '0',
'walton' => ''
);
$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;
试试这个
$mobiles = array(
'iPhone' => '0',
'tablet' => '0',
'samsung' => '0',
'walton' => ''
);
$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;
已更新:正如您所说,无需使用其他功能即可做到这一点。
不检查手机功能就搞定
$mobiles = array(
'iPhone' => '0',
'tablet' => '0',
'samsung' => '0',
'walton' => ''
);
$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;
我想得到的是..
我不知道 check_mobile_exists( $mobiles, $electronics )
这个函数一直返回 true。当 $electronics 键列表中存在 $mobile 数组键时我想要 true,当不存在时我想要 false。在此结果基础上,我正在循环 post 结果。但正如我所说,该函数在每次迭代时都返回 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' => '0',
'tablet' => '0',
'samsung' => '0',
'walton' => ''
);
$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;
试试这个
$mobiles = array(
'iPhone' => '0',
'tablet' => '0',
'samsung' => '0',
'walton' => ''
);
$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;
已更新:正如您所说,无需使用其他功能即可做到这一点。
不检查手机功能就搞定
$mobiles = array(
'iPhone' => '0',
'tablet' => '0',
'samsung' => '0',
'walton' => ''
);
$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;