Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. $chunk = 4;
  2.  
  3. $arr = array('1', '1', '1', '1', '1', '1', '1', '0', '1', '0', '1', '0', '0', '0', '0', '1');
  4.  
  5. 1110
  6. 1101
  7. 1110
  8. 1001
  9.  
  10. there are 4 number '1' in first column, so the output should be 4
  11.  
  12. function array_chunk_vertical($arr, $percolnum){
  13. $n = count($arr);
  14. $mod = $n % $percolnum;
  15. $cols = floor($n / $percolnum);
  16. $mod ? $cols++ : null ;
  17. $re = array();
  18. for($col = 0; $col < $cols; $col++){
  19. for($row = 0; $row < $percolnum; $row++){
  20. if($arr){
  21. $re[$row][] = array_shift($arr);
  22. }
  23. }
  24. }
  25. return $re;
  26. }
  27.  
  28. $result = array_chunk_vertical($arr, $chunk);
  29. $new = 0;
  30. $exist = 0;
  31.  
  32. foreach($result as $row){
  33. foreach($row as $val){
  34. if($val == "1"){
  35. $new++;
  36. }
  37. else{
  38. $exist++;
  39. }
  40. echo $val;
  41. }
  42. echo '<br/>';
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement