2011-10-14 32 views
0

我有一个数组montant将单个php数组转换为mutilple dim数组?

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

回答

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

感谢我的英雄! :) – sophie

0

你有一些错误$总数组定义内交配,纠正:

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