1

我将两个数组传递给一个函数,然后我想遍历第一个数组并对照第二个数组检查一些值。问题是第二个数组给出了奇怪的值并破坏了逻辑。请有人指出我在哪里愚蠢吗?

功能

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

希望你能帮忙

4

3 回答 3

1

我认为你的循环应该是:

foreach($values as $a){
       if(!isset($check[$a]) || empty($check[$a])) {
          return '<span class="w"><i>incomplete</i></span>';
          break;
       }
    $i++;
}
于 2012-08-16T12:04:27.577 回答
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>'; 
    }
}
于 2012-08-16T12:16:56.837 回答
0
  1. CompanyInfo 数组未定义
  2. $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>';
    
    }
    
于 2012-08-16T12:21:35.177 回答