对多维数组的行进行分组,以从 2 列创建分层键,然后从非识别数据创建子数组

问题描述 投票:0回答:4

我有一个像这样的 PHP 数组,我想将它们按

created_month
seq
进行分组,我尝试过合并和其他方法,但它不起作用。

"data": [
    {
        "seq": "1",
        "created_month": "Aug",
        "descr": "[YOU] - Not Recieved",
        "paydescr": "Revenue",
        "amount": "205.85"
    },
    {
        "seq": "1",
        "created_month": "Jul",
        "descr": "[YOU] - Not Recieved",
        "paydescr": "Revenue",
        "amount": "176.82"
    },
    {
        "seq": "2",
        "created_month": "Aug",
        "descr": "[OT] - Not Recieved",
        "paydescr": "Commission",
        "amount": "74.19"
    },
    {
        "seq": "2",
        "created_month": "Jul",
        "descr": "[OT] - Not Recieved",
        "paydescr": "Commission",
        "amount": "63.71"
    }
]

想要像这样显示它,这将按创建的月份和Seq进行分组

[{"data":[  
     {  
        "Aug":[  
           {  
     "1":[  
           { 
            "descr": "[YOU] - Not Received",
            "paydescr": "Revenue",
            "amount": "0.00"
            }
    ],
    "2":[  
           {"descr": "[YOU] - Not Received",
            "paydescr": "Revenue",
            "amount": "0.00"
            }
     ]
           }
        ]
     }
  ]

} ]

php arrays multidimensional-array grouping hierarchical-data
4个回答
1
投票

使用 array_reduce 可以解决你的问题。

$data = '{"data": [{
    "seq": "1",
    "created_month": "Aug",
    "descr": "[YOU] - Not Recieved",
    "paydescr": "Revenue",
    "amount": "205.85"
}, {
    "seq": "1",
    "created_month": "Jul",
    "descr": "[YOU] - Not Recieved",
    "paydescr": "Revenue",
    "amount": "176.82"
}, {
    "seq": "2",
    "created_month": "Aug",
    "descr": "[OT] - Not Recieved",
    "paydescr": "Commission",
    "amount": "74.19"
}, {
    "seq": "2",
    "created_month": "Jul",
    "descr": "[OT] - Not Recieved",
    "paydescr": "Commission",
    "amount": "63.71"
}]}';

$data = json_decode($data, true);

$group = array_reduce($data['data'], function ($old, $new) {
    $key = "{$new['created_month']}_{$new['seq']}";
    unset($new['created_month'], $new['seq']);
    $old[$key][] = $new;

    return $old;
}, []);

print '<pre>';
print_r($group); // Ass array
print_r(json_encode($group)); // As JSON

1
投票

另一种简单的

foreach()
循环方式,

foreach($array['data'] as $k=>$v){
    $result['data'][$v['created_month']][$v['seq']] = [$v['descr'],$v['paydescr'],$v['amount']];
}

工作演示: https://3v4l.org/h76I2


0
投票

使用这个 php 代码:

$data = '{"data": [{
    "seq": "1",
    "created_month": "Aug",
    "descr": "[YOU] - Not Recieved",
    "paydescr": "Revenue",
    "amount": "205.85"
}, {
    "seq": "1",
    "created_month": "Jul",
    "descr": "[YOU] - Not Recieved",
    "paydescr": "Revenue",
    "amount": "176.82"
}, {
    "seq": "2",
    "created_month": "Aug",
    "descr": "[OT] - Not Recieved",
    "paydescr": "Commission",
    "amount": "74.19"
}, {
    "seq": "2",
    "created_month": "Jul",
    "descr": "[OT] - Not Recieved",
    "paydescr": "Commission",
    "amount": "63.71"
}]}';

$data = json_decode($data, true);
$newArr = array();
foreach($data['data'] as $k=>$v){
    $newArr[$v['created_month']][$v['seq']][] = array("descr"=>$v['descr'],"paydescr"=>$v['paydescr'],"amount"=>$v['amount']);
}
echo json_encode($newArr);

0
投票

假设您的初始 json 字符串位于 $data 变量中。然后就可以使用下面的代码了


$formatArray = array();
foreach( $data['data'] as $d ) {
    $month = $d['created_month'];
    $seq = $d['seq'];

    if( !isset( $formatArray[$month] ) ) {
        $formatArray[$month] = array(); 
    }
    unset($d['created_month']);
    unset($d['seq']);

    $formatArray[$month][$seq] = $d;
}

$finalArray = array();
$finalArray['data'] = $formatArray;
echo json_encode($finalArray);
© www.soinside.com 2019 - 2024. All rights reserved.