将单个php数组转换为多个dim数组


Transform single php array to mutilple dim array?

我有一个数组montant

$montant = array(
    "EUR_credit"=>10, "USD_credit"=>20, "EUR_debit"=>30, "JPY_debit"=>20
);

I am trying

$total = array();
foreach ($montant as $key=>$value){
    $check_key = substr($key, 0,3);
    if(!isset($check_key)){
    }
}
echo '<pre>';
print_r($total);
echo '</pre>';
$total = array('EUR'=>array('credit'=10,'debit'=>30),
               'USD'=>array('credit'=20,'debit'=>NULL),
               'JPY'=>array('credit'=NULL,'debit'=>20),
      )
$total = array();
foreach ($montant as $type => $value) {
    list($currency, $type) = explode('_', $type);
    $total[$currency][$type] = $value;
    $total[$currency] += array('credit' => null, 'debit' => null);
}

$total数组定义中有一些错误,请更正:

$total = array
(
'EUR' => array('credit'=>10,'debit'=>30),
'USD' => array('credit'=>20,'debit'=>NULL),
'JPY' => array('credit'=>NULL,'debit'=>20),
);