У меня есть следующий многомерный массив. Обратите внимание, что payment_amount, payment_interest и payment_principle являются строками. Мне нужно привести эти значения к плавающим. Я все перепутал, может кто-нибудь предложить foreach (), который выполнит это, пожалуйста?
array (size=12)
0 =>
array (size=4)
'payment_date' => string '2015-01-01' (length=10)
'payment_amount' => string '159609.00' (length=9)
'payment_interest' => string '26697.78' (length=8)
'payment_principle' => string '132911.22' (length=9)
1 =>
array (size=4)
'payment_date' => string '2015-02-01' (length=10)
'payment_amount' => string '159609.00' (length=9)
'payment_interest' => string '25039.44' (length=8)
'payment_principle' => string '134569.56' (length=9)
2 =>
array (size=4)
'payment_date' => string '2015-03-01' (length=10)
'payment_amount' => string '159609.00' (length=9)
'payment_interest' => string '23356.20' (length=8)
'payment_principle' => string '136252.80' (length=9)
3 =>
array (size=4)
'payment_date' => string '2015-04-01' (length=10)
'payment_amount' => string '159609.00' (length=9)
'payment_interest' => string '21653.04' (length=8)
'payment_principle' => string '137955.96' (length=9)
4 =>
array (size=4)
'payment_date' => string '2015-05-01' (length=10)
'payment_amount' => string '159609.00' (length=9)
'payment_interest' => string '19929.96' (length=8)
'payment_principle' => string '139679.04' (length=9)
Как насчет этого?
foreach($array as $k=>$arr)
{
$array[$k]['payment_amount'] = (float) $arr['payment_amount'];
$array[$k]['payment_interest'] = (float) $arr['payment_interest'];
$array[$k]['payment_principle'] = (float) $arr['payment_principle'];
}
array_walk($arr, function(&$a) {
$a['payment_amount'] = (float) $a['payment_amount'];
$a['payment_interest'] = (float) $a['payment_interest'];
$a['payment_principle'] = (float) $a['payment_principle'];
});
ИЛИ ЖЕ
foreach ($arr as &$a) {
$a['payment_amount'] = (float) $a['payment_amount'];
$a['payment_interest'] = (float) $a['payment_interest'];
$a['payment_principle'] = (float) $a['payment_principle'];
}