0

Here is my code:

$convertTitle = $this->art->catList(); // gets an array which has other arrays as its values
    foreach($convertTitle as $cT){ // cycle through the smaller arrays
        if(in_array($cat, $cT)){ // if "xx" is found in a smaller array
            $data['title'] = $cT['full']; // set the $title to the full form corresponding to the abbreviated xx ($cat) 
        }
        if(!in_array($cat, $cT)){ 
            $data['title'] = "Sorry, an error occurred."; // if it's not found, choose an alternate title 
            $data['error'] = 1; // throw the error
        } 
    }

In order to debug, I've been printing in_array($cat, $cT) and when I expect it to, it outputs 1. When I expect it to, it outputs 0 as well. So it seems like it's working.I can even print($data['title']); and the correct title shows up! But regardless of whether in_array() is outputting 1 or 0, my second if statement always overrides the first and I $error always comes out 1. What gives?

Some solutions I've tried:

if(!in_array($cat, $cT)... 
if(in_array($cat, $cT) == false/0/null)...
else... 

I really have no idea why it's not outputting the proper title when the title is located in the variable when I want it to be!

EDIT: Here is print_r($convertTitle);

Array ( [0] => Array ( [handle] => dr [full] => Drawings ) [1] => Array ( [handle] => f [full] => Films & Stills ) [2] => Array ( [handle] => pa [full] => Paintings ) [3] => Array ( [handle] => ph [full] => Photography ) [4] => Array ( [handle] => po [full] => Portraits ) )
5
  • 1
    You should probably just use an else statement instead of another if because your code paths should be mutually exclusive. Also, can you include some examples of $cat and $cT? Commented Jun 5, 2014 at 18:12
  • Show me print_r($convertTitle);. Are you aware you can use if(...){...}else{...}? Commented Jun 5, 2014 at 18:12
  • I tried that and it still didn't work. Commented Jun 5, 2014 at 18:13
  • 1
    Once you set $data['error'] in the loop it will stay set. You're not handling that on subsequent loops. Commented Jun 5, 2014 at 18:14
  • what is stored in $cat? you are probably using in_array wrong Commented Jun 5, 2014 at 18:18

3 Answers 3

3

Why not just an else?

if (in_array()) {
   ...
} else {
   ...
}

There's basically NO point in having two separate if() tests when the conditions are boolean opposites of each other.

Plus, recall that your foreach() will iterate MANY items. For EVERY item that isn't in your array, you set $error to be TRUE. But then you don't reset that to FALSE when you do make a match. so

array item #1 -> not found, so error => true
array item #2 -> not found, so error => true
array item #3 -> found! -> don't change error, it's still true
array item ....
etc...
Sign up to request clarification or add additional context in comments.

1 Comment

@SanguineEpitaph Please think about Marc's last example. I think you have a logic problem instead of an in_array-problem.
0

You are overwriting your values in each iteration of the foreach loop, so you will end up with the result of the last value for the title and your error will remain set once it is set.

Apart from that, you should probably just write:

if (in_array()) {

} else {

}

Comments

0

Thanks for the answers! I forgot that the loop would be running over and over even after it found the proper entry. For future reference, here is the code which works:

$convertTitle = $this->art->catList(); 
    foreach($convertTitle as $cT){ 
        if(in_array($cat, $cT)){ 
            $data['title'] = $cT['full']; 
            $data['error'] = 0; 
            break; 
        }
        else{ 
            $data['title'] = "Sorry, an error occurred."; 
            $data['error'] = 1; 
        } 
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.