Comparison of Array() Results

Asked

Viewed 80 times

4

I have an Array() of results, I need to buy if the city of array 0 is equal to the city of array 1, if the city of array 1 is equal to the city of array 2... and so on.

My SQL print_r resulted in this:

Array
(
    [0] => Array
        (
            [tiposervico] => 0
            [endereco] => RUA CARLOS DE LAET
            [numero] => 1275
            [cidade] => 1
            [bairro] => 2
            [falarcom] => ADRIANO
            [idChamada] => 79
        )

    [1] => Array
        (
            [tiposervico] => 1
            [endereco] => 
            [numero] => 
            [cidade] => 1
            [bairro] => 11
            [falarcom] => 
            [idChamada] => 79
        )

    [2] => Array
        (
            [tiposervico] => 1
            [endereco] => 
            [numero] => 
            [cidade] => 2
            [bairro] => 316
            [falarcom] => 
            [idChamada] => 79
        )

    [3] => Array
        (
            [tiposervico] => 1
            [endereco] => 
            [numero] => 
            [cidade] => 2
            [bairro] => 327
            [falarcom] => 
            [idChamada] => 79
        )

    [4] => Array
        (
            [tiposervico] => 1
            [endereco] => 
            [numero] => 
            [cidade] => 1
            [bairro] => 21
            [falarcom] => 
            [idChamada] => 79
        )

)

Someone could tell me a way to elaborate this comparison in PHP?

I wrote the php code as follows:

        $contagemTrechoInicial = 0;
        for($contagemTrechos=0; $contagemTrechos<=count($dados)-1; $contagemTrechos++){
            $contagemTrechoInicial++;
            if($dados[$contagemTrechos]['cidade']==$dados[$contagemTrechoInicial]['cidade']) 
                echo "<br>Iguais > (".$dados[$contagemTrechos]['cidade'].")";   
            else echo "<br>Diferentes > (".$dados[$contagemTrechoInicial]['cidade'].")";
        }

But since it is $countTrechoInitial++; it always looks for an extra array, which generates an Undefined offset error: 5.

How can I adjust it to work properly?

Note: The result of this comparison should be:

Trecho 1: Cidade 1 > Cidade 1 - iguais
Trecho 2: Cidade 1 > Cidade 2 - diferentes
Trecho 3: Cidade 2 > Cidade 2 - iguais
Trecho 4: Cidade 2 > Cidade 1 - diferentes
  • Puts $contagemTrechoInicial++; after the else.

  • If I put it after Else, he says they’re all the same... And it’s not correct, it’s a multi-section comparison: the result should be: Stretch 1: City 1 > City 1 - equals Stretch 2: City 1 > City 2 - differing Stretch 3: City 2 > City 2 - equals Stretch 4: City 2 > City 1 - differing

  • Starts the variable with value 1. And leave after the else.

  • The mistake is the same as there... It gives the error in array 5, which does not exist, because in this case it goes up to 4... but other sections can be inserted, so it needs to be dynamic

  • 3

    check if the Dice exists before making the comparison, use isset(), if(isset($dados[$contagemTrechoInicial]['cidade']){ ...

  • With that isset of yours it worked

Show 1 more comment

1 answer

0


I noticed that you succeeded with @rray’s suggestion, but it follows a form alternative to make the snippet comparison algorithm.

$trechos = array(
        array("cidade" => 1),
        array("cidade" => 1),
        array("cidade" => 2),
        array("cidade" => 2),
        array("cidade" => 1)
);

$indiceAtual = 1;
$limite = count($trechos);

while ($indiceAtual < $limite) {
    $indiceAtual = $indiceAtual - 1;
    for ($x = 1; $x <= 2; $x++) {
        $cidade[] = $trechos[$indiceAtual]['cidade'];
        $indiceAtual++;
    }
    if ($cidade[0] == $cidade[1]) {
        echo 'iguais';
    }else{
        echo 'diferentes';
    }
    unset($cidade);
    echo '<br>';
}

Output:

iguais // 11
diferentes // 12
iguais // 22
diferentes // 21

Explaining the most important parts

The while runs while the total of array from parts not hit.

In the for I catch two snippets (cities) at a time and place each of them in the vector $cidade. So I can compare each couple of snippets at a time if.

Browser other questions tagged

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