Count how many times a string appears in an Object

2

I have an Object returned from the Facebook API and would like to "react" to the reactions of a post.

I have this snippet of code:

// Descodificando o JSON
    $objeto  = json_decode($userNode['reactions']);

// Percorrendo dados
    foreach( $objeto as $tipo )
    {
        echo $tipo->type."<br />";
    }

PS: var_dump($objeto);

array (size=6)
 0 => 
object(stdClass)[115]
  public 'id' => string '753855038121014' (length=15)
  public 'name' => string 'Ivone Kodama' (length=12)
  public 'type' => string 'LIKE' (length=4)
1 => 
object(stdClass)[113]
  public 'id' => string '392780707772557' (length=15)
  public 'name' => string 'Eunice Garcez' (length=13)
  public 'type' => string 'LIKE' (length=4)
2 => 
object(stdClass)[114]
  public 'id' => string '1355251021162174' (length=16)
  public 'name' => string 'Gih Andrea Anastacio De Carvalho Baracat' (length=40)
  public 'type' => string 'SAD' (length=3)
3 => 
object(stdClass)[54]
  public 'id' => string '408046949554084' (length=15)
  public 'name' => string 'Terezinha Piassa' (length=16)
  public 'type' => string 'LIKE' (length=4)
4 => 
object(stdClass)[51]
  public 'id' => string '194465404377613' (length=15)
  public 'name' => string 'Sueli Aparecida' (length=15)
  public 'type' => string 'LIKE' (length=4)

I get the following:

WOW WOW LIKE LIKE HAHA WOW LIKE LIKE SAD LIKE LIKE HAHA

Etc ... How can I "know" how many times the word LIKE is repeated for example?

    
asked by anonymous 31.03.2017 / 16:11

3 answers

7

If you do not have a special reason to work with objects simply simplify by returning an array in json_decode() . With this you can extract the values of the key type ( array_column() php5.5) and apply array_count_values() direct.

json example:

$arr = array(
         array('id' => '753855038121014', 'type' => 'LIKE'),
         array('id' => '392780707772557', 'type' => 'LIKE'),
         array('id' => '1355251021162174', 'type' => 'SAD'),
         array('id' => '408046949554084', 'type' => 'LIKE'),
         array('id' => '194465404377613', 'type' => 'LIKE')
       );

PHP > = 5.5

$arr  = json_decode($userNode['reactions'], true);
$termos = array_count_values(array_column($arr, 'type'));
echo $termos['LIKE'];

PHP < 5.5

$arr  = json_decode($userNode['reactions'], true);
$termos = array_map(function($item){ return $item['type']; }, $arr);
$termos = array_count_values($termos);
echo $termos['LIKE'];

You can assemble a summary as follows:

foreach($termos as $key => $value){
    $qtd = $value > 1 ? 'vezes' : 'vez';
    printf("%s: %s %s. <br>", $key, $value, $qtd);
}
echo 'TOTAL: '. array_sum($termos);

Output:

LIKE: 4 vezes.
SAD: 1 vez.
TOTAL: 5
    
31.03.2017 / 17:03
2

You can use the php function

example:

$array = ['a','b','c','b'];
$arrayCount = array_count_values(array_map('strtolower', $array));
$arrayCountB = $arrayCount['b']; // 2 
    
31.03.2017 / 16:46
1

You can use reduce to reduce your array to an array containing only the item and the quantity;

$reduce = array_reduce($objeto,function($carry, $item){
    if(empty($carry[$item->type])) $carry[$item->type] = 1;
    else $carry[$item->type]++;
    return $carry;
},[]);

echo json_encode($reduce); //{"LIKE":4,"SAD":1}
    
31.03.2017 / 16:47