Guest User

Untitled

a guest
Apr 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. data = [
  2. {
  3. "Payment_type":1,
  4. "amount":10000
  5. },
  6.  
  7. {
  8. "Payment_type":1,
  9. "amount":40000
  10. },
  11. {
  12. "Payment_type":2,
  13. "amount":10000
  14. },
  15. {
  16. "Payment_type":2,
  17. "amount":20000
  18. }
  19.  
  20. Payment type 1
  21. ---10000
  22. ---40000
  23. Payment type 2
  24. ---10000
  25. ---20000
  26.  
  27. foreach($data as $d)
  28. {
  29. if($d->Payment_type == 1)
  30. {
  31. echo $d->amount;
  32. }
  33. else
  34. {
  35. echo $d->amount;
  36. }
  37. }
  38.  
  39. function groupArray($arr, $group, $preserveSubArrays = false, $preserveGroupKey = false) {
  40. $temp = array();
  41. foreach($arr as $key => $subarray) {
  42. $groupBy = $subarray[$group];
  43. if(!$preserveGroupKey) {
  44. unset($arr[$key][$group]);
  45. }
  46. if(!array_key_exists($groupBy, $temp)) {
  47. $temp[$groupBy] = array();
  48. }
  49. if(!$preserveSubArrays && count($arr[$key])==1) {
  50. $temp[$groupBy][] = current($arr[$key]);
  51. } else {
  52. $temp[$groupBy][] = $arr[$key];
  53. }
  54. }
  55. return $temp;
  56. }
  57.  
  58. print_r(groupArray($data, "Payment_type", false, false));
  59.  
  60. Array
  61. (
  62. [1] => Array
  63. (
  64. [0] => 10000
  65. [1] => 40000
  66. )
  67.  
  68. [2] => Array
  69. (
  70. [0] => 10000
  71. [1] => 20000
  72. )
  73.  
  74. )
Add Comment
Please, Sign In to add comment