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

时间:2011-10-14 13:34:02

标签: php arrays

我有一个数组montant

$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),
      )

2 个答案:

答案 0 :(得分:1)

$total = array();
foreach ($montant as $type => $value) {
    list($currency, $type) = explode('_', $type);
    $total[$currency][$type] = $value;
    $total[$currency] += array('credit' => null, 'debit' => null);
}

答案 1 :(得分:0)

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

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