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

将单个php数组转换为多个dim数组?,php,arrays,Php,Arrays,我有一个数组蒙塔特 $montant = array( "EUR_credit"=>10, "USD_credit"=>20, "EUR_debit"=>30, "JPY_debit"=>20 ); 我正在努力 $total = array(); foreach ($montant as $key=>$value){ $check_key = substr($key, 0,3); if(!isset($check_key)){ }

我有一个数组蒙塔特

$montant = array(
    "EUR_credit"=>10, "USD_credit"=>20, "EUR_debit"=>30, "JPY_debit"=>20
);
我正在努力

$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作为$key=>$value){
$check_key=substr($key,0,3);
如果(!isset($check_key)){
}
}
回声';
印刷费(合计);
回声';
$total=array('EUR'=>array('贷方'=10,'借方'=>30),
'USD'=>数组('credit'=20,'debit'=>NULL),
'JPY'=>array('credit'=NULL,'debit'=>20),
)

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

$total = array();
foreach ($montant as $type => $value) {
    list($currency, $type) = explode('_', $type);
    $total[$currency][$type] = $value;
    $total[$currency] += array('credit' => null, 'debit' => null);
}
$total = array
(
'EUR' => array('credit'=>10,'debit'=>30),
'USD' => array('credit'=>20,'debit'=>NULL),
'JPY' => array('credit'=>NULL,'debit'=>20),
);