How to escape monetary value commas in a php array?

Asked

Viewed 68 times

-2

How to escape monetary value commas in a php array? I have the array below with 12 positions but the comma that separates the decimals is giving me problems, php understands that it is a new position of the array and separates the value, how to get around it ?

data: [ 0,00,
        0,00,
        0,00,
        0,00,
        0,00,
        0,00,
        0,00,
        0,00,
        2.468,00,
        2.093,00,
        600,00,
        0,00
]
  • 2

    There is no way to mount this array with the values in quotes?

  • How is this being assembled array?

  • Leaves values as string

  • 1

    Nor should there be a comma to start the conversation. Decimal in PHP is separated by a dot (and there are no quotes). Even better would be inform with integers, since it is monetary (inform in cents) and format only in display. Now, if you are receiving this data, convert to the input (but there is already a completely different problem than what was exposed in the question)

1 answer

-1

You can use something like:

$data = [
    '0,00',
    '0,00',
    '0,00',
    '0,00',
    '0,00',
    '0,00',
    '0,00',
    '0,00',
    '2.468,00',
    '2.093,00',
    '600,00',
    '0,00'
 ];

$valores = array_map(function($valor){
   return str_replace(",",".",str_replace(".","",$valor))
},$data);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.