vitozy

Laravel Collection - math functions

Apr 13th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.77 KB | None | 0 0
  1. $nums = Collection::make([5, 7, 1, 5, 9, 11, 3, 2, 1, 1, 7, 5]);
  2.  
  3. $nums->avg(); //átlag: 4.75
  4. $nums->sum(); //összeg: 57
  5. $nums->max(); //max érték: 11
  6. $nums->count(); //mennyiség: 12
  7. $nums->countBy(); //miből mennyi van? (Collection az eredmény, [5 => 3, 7 => 2, ...])
  8.  
  9. $nums->random(); //random szám
  10. $nums->random(3); //3 db random szám
  11. $nums->shuffle(); //megkeveri random
  12.  
  13. $stat = Collection::make([
  14.     ['apple' => 100, 'pear' => 56, 'cherry' => 12],
  15.     ['apple' => 45, 'pear' => 157, 'cherry' => 22],
  16.     ['apple' => 3, 'pear' => 75, 'cherry' => 33],
  17.     ['apple' => 19, 'pear' => 13, 'cherry' => 33]
  18. ]);
  19.  
  20. $stat->sum('apple'); //almák összesen: 167
  21. $stat->median('pear'); //körték mediánja: 65.5
  22. $stat->mode('cherry'); //cseresznyék módusza: [33]
Add Comment
Please, Sign In to add comment