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 theelse
.– Diego Souza
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
– Sr. André Baill
Starts the variable with value
1
. And leave after theelse
.– Diego Souza
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
– Sr. André Baill
check if the Dice exists before making the comparison, use
isset()
,if(isset($dados[$contagemTrechoInicial]['cidade']){ ...
– rray
With that isset of yours it worked
– Sr. André Baill