1

I have the following multidimensional array. Notice that payment_amount, payment_interest, and payment_principle are strings. I need to cast these values to floats. I am getting all twisted around, can someone suggest a foreach() that will accomplish this please?

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)
7
  • 1
    php is loosely typed, do your really need to change the type? show us how your useing the data Commented Jan 20, 2015 at 22:55
  • 1
    show us what you tried Commented Jan 20, 2015 at 22:57
  • Yes, because I'm using a package that is taking the array and exporting to an Excel format. When I open the downloaded file in Excel is seeing the data type as text and the formatting as currency isn't working. Commented Jan 20, 2015 at 22:57
  • foreach($payments as $record) { foreach($record as $key => $value) { $payments[$key] = (float) $value; $payments[$key] = (float) $value; $payments[$key] = (float) $value; } } Commented Jan 20, 2015 at 22:59
  • sounds like an issue woth the code your using to create the excel file Commented Jan 20, 2015 at 22:59

2 Answers 2

3

What about this?

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'];
}
Sign up to request clarification or add additional context in comments.

Comments

1
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'];
});

OR

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'];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.