按键和值将PHP数组分组

时间:2019-07-15 05:20:35

标签: php json

我有一个这样的PHP数组,我想按created_month和seq对其进行分组,我尝试了merge和其他方法,但它不起作用

"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"
            }
     ]
           }
        ]
     }
  ]

} ]

4 个答案:

答案 0 :(得分: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 :(得分: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

答案 2 :(得分: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);

答案 3 :(得分: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);