Error checking IF with array in PHP

Asked

Viewed 437 times

0

I’m trying to make a if within a foreach and the situation is as follows:

I have a array who I called $plates who returns like this:

array(2) { [0]=> array(1) { ["chave"]=> string(1) "1" } [1]=> array(1) { ["chave"]=> string(1) "3" } }

And I tried this code:

foreach($matriculas as $matricula){
        if(in_array($row['id_curso'],$matricula)){
            echo '<a href="" class="btn btn-success disabled">Matriculado</a>';
        }else{
            echo 'nois';
        }
    }

I would like to show the information if the value of $row['id_curso'] exist in $matricula, if it does not exist it should show any button. The problem is that it displays both, ie existing $row['id_curso'] in $matriculas or not existing. There is a way to correct this?

  • You don’t need to iterate the array $matriculas with the foreach the function in_array already checks whether or not there is!

  • @Igormello if I don’t use foreach it won’t find because the array is Multi Dimensional... If it were a simple array it would run!

  • my mistake, had not noticed this. I made a possible solution take a look!

4 answers

2

The problem is in logic. You are "printing" the two results by the fact of your array be multidimensional, then for each array intern he tests the if/else even though the value has already been found in the first iteration, it will do the test until the arrays interns.

Here’s a possible solution:

$matriculas = array(array('chave'=>1), array('chave'=>3));
$row['id_curso'] = 3;
$saida = false;

foreach($matriculas as $matricula){
    if(!$saida)
        $saida = (in_array($row['id_curso'],$matricula));
}
echo ($saida == 1) ? '<a href="" class="btn btn-success disabled">Matriculado</a>' : 'qualquer coisa';

I used an auxiliary variable called $saida to control. It starts with false. So for every turn of the foreach I test to see if "found" something, because if she gets true(found the element) is because you no longer need to take the test.

  • Yeah, I just tried this case... If you did not declare the output value (which you would receive True if you found it) to be false, it would mark all courses as enrolled, you are right!

  • It is like this with this variable makes the control of the foreach, until when he has to search for the element in the array!

2

That would work too!

$matriculas = array(array('chave'=>1), array('chave'=>3));

    foreach($matriculas as $obj){
        foreach ($obj as $value){
            if($value == $row['id_curso']){
                echo '<a href="" class="btn btn-success disabled">Matriculado</a>';
            }
            elseif ($value =! $row['id_curso']){
                echo 'nois';
            }
            }
        }

1

What happens is, for each item in the list it checks, then it will see there, the array has 1, 3, 5, 7. it takes element id 5

He’ll be talking, no, no, yes, no. What you need to do is find the element in there with the loop, and exit the loop with the information whether you found it or not, then take action.

foreach($chaves as $arrayChaves){
    if(in_array($row['chave'],$arrayChaves, false)){
        achou = true;
        break;
    }
}
if($achou) {
    echo '<a href="" class="btn btn-success disabled">Matriculado</a>';
}else{
    echo 'nois';
}

So it worked perfectly, but thanks to everyone who tried to help.

  • Place break may even have worked, but it’s not good practice!

  • Right, in case, I just used Break to force him to stop there and go back to the beginning pass to the next foreach character...

0

I tested the following code and worked correctly:

<?php
$matriculas = array(array('chave'=>1), array('chave'=>3));

foreach($matriculas as $matricula){
        if(in_array(1,$matricula)){
            echo 'Encontrado';
        }else{
            echo 'Não Encontrado';
        }
    }
?>

In your case:

<?php
$matriculas = array(array('chave'=>1), array('chave'=>3));

foreach($matriculas as $matricula){
        if(in_array($row['id_curso'],$matricula)){
            echo 'Encontrado';
        }else{
            echo 'Não Encontrado';
        }
    }
?>

If not, check the value of $row['id_curso'] if that’s correct.

The content of $matriculas has to be a array ? For there may be other ways to do what you need.

  • did not work, actually I could return as an object, I brought as an array by being the default that I am using in this project... Could you give me an example of how to do this using objects? Only one detail, it works the right way, the problem is that it is printing both the if positive value of the IF when the negative value, and it looks like this, for example "Found" you understand? and in what does not find it says "NOT FOUND"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.