1
something strange is occurring in the output of my function. When using var_dump only in the variable it returns key and value normally. Piece of code:
public function buildGender()
{
$unknow_name = array();
foreach ( $this->author_comment as $key => $value)
{
$first_name = split(' ',$value);
if ( isset( $this->genders[ trim( $first_name[ 0 ] ) ] ) )
{
$this->gender_comment[$key] = $this->genders[$first_name[0]];
}
else
{
$this->gender_comment[$key] = '0';
$unknow_name[] = $first_name[0];
}
}
foreach ( $this->author_reply as $key => $value)
{
$first_name = split( ' ',$value );
if ( isset( $this->genders[ trim( $first_name[0] ) ] ) )
{
$this->gender_reply[$key] = $this->genders[$first_name[0]];
}
else
{
$this->gender_reply[$key] = '0';
$unknow_name[] = $first_name[0];
}
}
foreach ( $unknow_name as $key )
{
$PDO = Database::connect();
$SQL = "INSERT INTO sys_name_gender ( name,classification ) VALUES (?,?) on conflict(name) do nothing";
$DATA = array($key,'0');
$SQL = $PDO->prepare($SQL);
$SQL->execute($DATA);
$PDO = Database::disconnect();
}
//var_dump(array_count_values($this->gender_comment) );
echo json_encode( array( 'gender_reply' => array_count_values( $this->gender_reply ), 'gender_comment' => array_count_values( $this->gender_comment ) ) );
}
This function takes the first name, compares it with my sorted name base and if it returns a string 0 | 1 | 2
. If it is 0 it inserts the name to be classified in the future.
The way out is like this:
{"gender_reply":{"1":3,"0":24,"2":4},"gender_comment":[5341,358,3478]}
would have to stay that way:
{"gender_reply":{"1":3,"0":24,"2":4},"gender_comment":{"1":358,"0":5341,"2":3478}}
in var_dump it correctly shows the keys and values, taking only json_encode
Does anyone know why he formatted this JSON like this?
was like this:
$_gender_reply = array_count_values( $this->gender_reply );
$_gender_comment = array_count_values( $this->gender_comment );
$_zero = $_gender_comment['0']+$_gender_reply['0'];
$_one = $_gender_comment['1']+$_gender_reply['1'];
$_two = $_gender_comment['2']+$_gender_reply['2'];
$total = $_zero+$_one+$_two;
echo json_encode( array( 'total' => $total-$_zero, '%homem' => number_format($_one/($total-$_zero)*100,'2'), '%mulher' => number_format($_two/($total-$_zero)*100,'2') ) );
It worked perfectly, but I’d like to know why he didn’t mount JSON the right way.
Show an example of how to return. Also show an example of what the var_dump test returns()
– Daniel Omine
Daniel, I appreciate your interest in helping. In the var_dump it normally shows the key output and value of both, I checked the situation by making one more filter and took the opportunity to put the calculated fields instead of dealing with js. But it is strange this kind of bug, I still investigate the cause of it. I will update in the post.
– Christopher Tavares