Error checking the IF with array in PHP

0

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

I have array that I called $ enrollments which 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 display the information if the value of $row['id_curso'] exists in $matricula , if it does not exist it should show a button whatever. The problem is that it displays both, ie, $row['id_curso'] exists in $matriculas or not existing. Is there a way to fix this?

    
asked by anonymous 18.07.2016 / 23:58

4 answers

2

The problem is in logic. You are "printing" the two results because your array is multidimensional, so for every% internal% it tests the array even the value already found in the first iteration, it will test until the % internal%.

Here is 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 if/else to control. It starts with arrays . So for each round of $saida I test to see if I "found" something, because if it receives false (found the element) it is because it does not need to test anymore.

    
19.07.2016 / 03:21
2

That would work as well!

$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';
            }
            }
        }
    
19.07.2016 / 03:26
1

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

He will pass by speaking, no, no, yes, no. What you need to do is find the element in there with the loop, and leave the loop with the information whether or not it is found, 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.

    
19.07.2016 / 03:13
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 it does not, check the value of $row['id_curso'] if it is correct.

Does the contents of $matriculas have to be array ? For there may be other ways of doing what you need.

    
19.07.2016 / 00:44