我将两个数组传递给一个函数,然后我想循环遍历第一个数组并检查第二个数组的某些值。问题是第二个数组给出了奇怪的值并打破了逻辑。请有人指出我愚蠢的地方吗?
功能
function completion($check, $values){
$num = count($values);
$i=0;
foreach($values as $a){
foreach($check as $b){
if($b[$a] == ''){
return '<span class="w"><i>incomplete</i></span>';
break;
}
}
$i++;
}
if ($i == $num);
return;
}
$values = array('short_bio', 'industry_sector', 'profile_status', 'country', 'locations', 'noe');
$check = ( [row] => 1 [user_id] => 2 [company_name] => mylittlefish [industry_sector] => Automotive / Aerospace [job_title] => Director [profile_status] => [first_name] => Joe [last_name] => Hodgson [package] => [sector] => Catering [recruitment_status] => Keeping an ear to the ground [country] => UK [locations] => Doncaster [noe] => 5 [user_recruitment_status] => [user_endorsements] => [short_bio] => test [previous_job_title] => [summary] => [profile] => [cover] => )
如果我回复$b[$a]
我得到这样的东西:
612mADJHCKUD5t12mADJHCKUD5t12mADJHCKUD5t12mADJHCKUD5t12mADJHCKUD5t12mADJHCKUD5t6
希望你能帮忙
乔
答案 0 :(得分:1)
我认为你的循环应该是:
foreach($values as $a){
if(!isset($check[$a]) || empty($check[$a])) {
return '<span class="w"><i>incomplete</i></span>';
break;
}
$i++;
}
答案 1 :(得分:0)
function completion($check, $values){
foreach($values as $a){
foreach($check as $b=>$value){
if($b == $a && $value == ''){
return '<span class="w"><i>incomplete</i></span>';
}
}
}
}
或
function completion($check, $values){
foreach($values as $a){
if (array_key_exists($a,$check) && $check[$a] == '') return '<span class="w"><i>incomplete</i></span>';
}
}
答案 2 :(得分:0)
$ check数组缺少逗号,字符串值未包含在引号
中$companyInfos = array();
$companyInfo[0] = array( "row" => 1,
"short_bio" => "Painter", "status" => "Single");
$companyInfo[0] = array( "row" => 2,
"short_bio" => "Designer", "status" => "Married");
$values = array('short_bio', 'industry_sector',
'profile_status', 'country', 'locations', 'note');
foreach($companyInfos as $companyInfo)
foreach($values as $val)
{
if($companyInfo[$val] =='')
echo '<span class="w"><i>incomplete</i></span>';
}