jhodgson4
jhodgson4

Reputation: 1656

iterating through an array then through another giving strange values

I am passing two arrays to a function, I am then wanting to loop through the first array and check some values against the second. The problem is the second array is giving strange values and breaking the logic. Please could someone point out where I'm being stupid?

the function

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] => )

if I echo $b[$a] i get something like this:

612mADJHCKUD5t12mADJHCKUD5t12mADJHCKUD5t12mADJHCKUD5t12mADJHCKUD5t12mADJHCKUD5t6

Hope you can help

Joe

Upvotes: 1

Views: 66

Answers (3)

Festim
Festim

Reputation: 201

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>';                   
           }
       }

    }
}

or

function completion($check, $values){
    foreach($values as $a){
        if (array_key_exists($a,$check) && $check[$a] == '') return '<span class="w"><i>incomplete</i></span>'; 
    }
}

Upvotes: 0

Naveen Kumar
Naveen Kumar

Reputation: 4601

  1. CompanyInfo array is not defined
  2. $check array is missing commas and string values are not enclosed in quotes

    $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>';
    
    }
    

Upvotes: 0

Oussama Jilal
Oussama Jilal

Reputation: 7739

I think your loop should be :

foreach($values as $a){
       if(!isset($check[$a]) || empty($check[$a])) {
          return '<span class="w"><i>incomplete</i></span>';
          break;
       }
    $i++;
}

Upvotes: 1

Related Questions