Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- data = [
- {
- "Payment_type":1,
- "amount":10000
- },
- {
- "Payment_type":1,
- "amount":40000
- },
- {
- "Payment_type":2,
- "amount":10000
- },
- {
- "Payment_type":2,
- "amount":20000
- }
- Payment type 1
- ---10000
- ---40000
- Payment type 2
- ---10000
- ---20000
- foreach($data as $d)
- {
- if($d->Payment_type == 1)
- {
- echo $d->amount;
- }
- else
- {
- echo $d->amount;
- }
- }
- function groupArray($arr, $group, $preserveSubArrays = false, $preserveGroupKey = false) {
- $temp = array();
- foreach($arr as $key => $subarray) {
- $groupBy = $subarray[$group];
- if(!$preserveGroupKey) {
- unset($arr[$key][$group]);
- }
- if(!array_key_exists($groupBy, $temp)) {
- $temp[$groupBy] = array();
- }
- if(!$preserveSubArrays && count($arr[$key])==1) {
- $temp[$groupBy][] = current($arr[$key]);
- } else {
- $temp[$groupBy][] = $arr[$key];
- }
- }
- return $temp;
- }
- print_r(groupArray($data, "Payment_type", false, false));
- Array
- (
- [1] => Array
- (
- [0] => 10000
- [1] => 40000
- )
- [2] => Array
- (
- [0] => 10000
- [1] => 20000
- )
- )
Add Comment
Please, Sign In to add comment