Count how many times a string appears in an Object

Asked

Viewed 1,606 times

2

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

I have this excerpt from the 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)

Return me the following:

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

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

  • If it is an array ( $objeto->type) can do so: Find duplicate values in an array

  • @rray not understood, could be a little more clear? Thank you

  • You can take the array that has these values and use the function array_count_values() it will return the number of times the words appeared. Just need to know exactly what is the key of the array/object to apply.

  • @rray look at the issue please.

  • @Only take the left-hand print: https://imageshack.us/i/pmmeNF0Gp

3 answers

6


If you don’t have a special reason to work with simplique objects by returning an array in the json_decode(). With this you can extract the values from the key type (array_column() php5.5) and apply the array_count_values() straightforward.

Example of json:

$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);

Exit:

LIKE: 4 vezes.
SAD: 1 vez.
TOTAL: 5
  • follows the output, as I can sum all values?

  • @Lucasbicalleto In the second code the array_count_values()

  • I’m using the first code, how can I count all the equal values returned in your answer ?

  • @Lucasbicalleto did you put this code inside the foreach? it should not repeat the values.

  • yes because I’m taking all posts, then I need to put inside the Foreach.

  • I’ll have to open another hole ??

  • @Lucasbicalleto needs to play the answer code after foreach, anything puts the code q vc has in Pastebin and passes the link.

  • https://pastebin.com/pXT57xeF

  • @Lucasbicalleto I think it’s best to ask another question.

  • got it, can you help me to formalize the question?

  • @Lucasbicalleto I don’t exactly what you want to count? is reactions by post? count all totaled independent of the post?

Show 6 more comments

2

You can use the this function php

example:

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

0

You can use reduce to reduce your array to an array containing only the item and 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}
  • It worked: without wanting to ask a lot, can you help me to "gather" everything ? That’s how it is {"LIKE":3,"ANGRY":1}{"LIKE":1}{"LIKE":7,"SAD":2}{"LIKE":12,"ANGRY":1,"HAHA":3,"WOW":6,"SAD":1}{"LIKE":18,"WOW":1}{"LIKE":24,"HAHA":1}{"LIKE":1}{"LIKE":20,"HAHA":1,"SAD":1,"LOVE":3}{"LIKE":5}{"LIKE":20,"SAD":1,"LOVE":2,"ANGRY":1,"WOW":1}{"LIKE":24,"LOVE":1}{"LIKE":1}{"LIKE":24,"WOW":1}{"LIKE":10}{"LIKE":2}{"LIKE":18,"SAD":2,"WOW":1,"ANGRY":4}{"HAHA":15,"WOW":1,"LIKE":9}{"LIKE":8}{"LIKE":20,"HAHA":3,"LOVE":2}{"LIKE":24,"LOVE":1}{"LIKE":25}{"LIKE":20,"ANGRY":2,"WOW":2,"SAD":1}

  • You are running in a loop and come various objects, dai vc want to join all of them?

  • That, join in a way that is more like this: {"LIKE":100,"ANGRY":20 etc.... } All in just 1

  • In this case you can use a merge array_to make all the items in all arrays turn into one array, then you can continue using the above reduce array_reduce , or group everything into one variable , doing something like this foreach($reduce as $type => $value){ if(Empty($groupType[$type])) $groupType[$type] = $value; Else $groupType[$type] += $value; } Where $groupType is the variable that will group items,

Browser other questions tagged

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