Felipe Bueno Felipe Bueno - 7 months ago 18
PHP Question

Count results of a multidimensional array

I have a newbie question here, but I'm really stucked. I have a multidimensional array that comes from a SELECT from a database, what I need is pretty simple, I'm doing a graphic where contains the number of results of each column of every user specified.

Heres some code to clarify your minds:

try {
$pdo = new PDO('mysql:host=***; dbname=**;charset=utf8;', '***', '**');

}catch(PDOException $err){
echo "Erro:\n".$err->getMessage();
}

$sql = "SELECT * FROM results
WHERE ra like '****'";

$statement = $pdo->prepare($sql);
$statement->execute();
$result = $statement->fetchAll();

$resultPgto = array_column($result, 'indicado_pgto');

print_r($resultadoPgto);


This returns me this array:

array(3) {
[0]=>
string(18) "Indicacao Invalida"
[1]=>
string(3) "Nao"
[2]=>
string(3) "Nao"
}


I need to count the results that are equal to
Sim
AND it should returns me 0 but instead when I do something like
print_r($resultadoPgto == 'Sim');
it returns '1'...

Other funny fact is, when I try to return results that are equal to
Nao
(theres 2 there) it returns me 0 or 1...

What I'm missing here? Can someone please help?

EDIT 1 :

Counting the result itself will return the number of keys the array has.

Here is the full Result to your understanding.

array(3) {
[0]=>
array(22) {
["dsadsad"]=>
string(7) "***"
[0]=>
string(7) "****"
["nome_aluno"]=>
string(25) "lore"
[1]=>
string(25) "ipsum"
["nome_indicado"]=>
string(25) "lore"
[2]=>
string(25) "ipsum"
["indicado_inscrito"]=>
string(18) "Indica��o Inv�lida"
[3]=>
string(18) "Indica��o Inv�lida"
["indicado_confirmado"]=>
string(18) "Indica��o Inv�lida"
[4]=>
string(18) "Indica��o Inv�lida"
["indicado_presente"]=>
string(18) "Indica��o Inv�lida"
[5]=>
string(18) "Indica��o Inv�lida"
["indicado_aprovado"]=>
string(18) "Indica��o Inv�lida"
[6]=>
string(18) "Indica��o Inv�lida"
["indicado_matriculado"]=>
string(18) "Indica��o Inv�lida"
[7]=>
string(18) "Indica��o Inv�lida"
["indicado_pgto"]=>
string(18) "Indicacao Invalida"
[8]=>
string(18) "Indicacao Invalida"
["validacao_indicacao"]=>
string(3) "N�o"
[9]=>
string(3) "N�o"
["validacao_desconto"]=>
string(3) "N�o"
[10]=>
string(3) "N�o"
}
[1]=>
array(22) {
["dsdsdasda"]=>
string(7) "***"
[0]=>
string(7) "***"
["nome_aluno"]=>
string(25) "lore"
[1]=>
string(25) "ipsum"
["nome_indicado"]=>
string(23) "lore"
[2]=>
string(23) "ipsum"
["indicado_inscrito"]=>
string(3) "Sim"
[3]=>
string(3) "Sim"
["indicado_confirmado"]=>
string(3) "N�o"
[4]=>
string(3) "N�o"
["indicado_presente"]=>
string(9) "EM ABERTO"
[5]=>
string(9) "EM ABERTO"
["indicado_aprovado"]=>
string(9) "EM ABERTO"
[6]=>
string(9) "EM ABERTO"
["indicado_matriculado"]=>
string(3) "N�o"
[7]=>
string(3) "N�o"
["indicado_pgto"]=>
string(3) "Nao"
[8]=>
string(3) "Nao"
["validacao_indicacao"]=>
string(3) "Sim"
[9]=>
string(3) "Sim"
["validacao_desconto"]=>
string(3) "N�o"
[10]=>
string(3) "N�o"
}
[2]=>
array(22) {
["asdasdsad"]=>
string(7) "***"
[0]=>
string(7) "***"
["nome_aluno"]=>
string(25) "lore"
[1]=>
string(25) "ipsum"
["nome_indicado"]=>
string(15) "lore"
[2]=>
string(15) "ipsum"
["indicado_inscrito"]=>
string(3) "Sim"
[3]=>
string(3) "Sim"
["indicado_confirmado"]=>
string(3) "N�o"
[4]=>
string(3) "N�o"
["indicado_presente"]=>
string(9) "EM ABERTO"
[5]=>
string(9) "EM ABERTO"
["indicado_aprovado"]=>
string(9) "EM ABERTO"
[6]=>
string(9) "EM ABERTO"
["indicado_matriculado"]=>
string(3) "N�o"
[7]=>
string(3) "N�o"
["indicado_pgto"]=>
string(3) "Nao"
[8]=>
string(3) "Nao"
["validacao_indicacao"]=>
string(3) "Sim"
[9]=>
string(3) "Sim"
["validacao_desconto"]=>
string(3) "N�o"
[10]=>
string(3) "N�o"
}
}


The only field I need information is
"indicado_pgto"
, thats why I did the
array_column()


What I really need is compare each value and count them, to return me any position that have the string
'Sim'
on it, and even if don't have any
'Sim'
string on it, it returns me 1

Answer

If you have this array after using array_column() on the SQL results, then the assumption is that your target array looks like this:

array(3) {
  [0]=>
  string(18) "Indicacao Invalida"
  [1]=>
  string(3) "Nao"
  [2]=>
  string(3) "Nao"
}

If so, then you should be able to do something simple like this?

<?php

$counter = 0;

foreach ($resultadoPgto as $result) {
    if ($result === 'Nao') {
        $counter++;
    }
}

print_r($counter);

Note that I used the triple equality operator === instead of the double equality operator==.

The weird behaviour you are experiencing with your print_r($resultadoPgto == 'Sim') returning '1' or '0' could be an side effect of using the double equality operator since it does not take the data type in consideration when evaluating the expression.