-2

我有这个数组:

array (
  1 => 
  array (
    'name' => 'Product 1',
    'price' => '5',
  ),
  2 => 
  array (
    'name' => 'Product 2',
    'price' => '$10',
  ),
  3 => 
  array (
    'name' => 'Product 3',
    'price' => '$50',
  ),
  4 => 
  array (
    'name' => 'Product 4',
    'price' => '20',
  ),
)

我需要遍历这个数组,将所有价格重新格式化为十进制格式。例如,10 是 10.00,50 是 50.00。如果用户提交 $50 的值,我还需要确保 $ 已被删除

这些值被替换后。我需要一个在 $result 的值中看起来像这样的数组,所以它应该看起来像:

array (
  1 => 
  array (
    'name' => 'Product 1',
    'price' => '5.00',
  ),
  2 => 
  array (
    'name' => 'Product 2',
    'price' => '10.00',
  ),
  3 => 
  array (
    'name' => 'Product 3',
    'price' => '50.00',
  ),
  4 => 
  array (
    'name' => 'Product 4',
    'price' => '20.00',
  ),
)

谢谢,所有的帮助!

4

5 回答 5

0
foreach ($array as &$element) {
  $element['price'] = sprintf("%.2f", str_replace('$', '', $element['price'];
}

放在&迭代变量之前使其成为对实际元素的引用而不是副本,因此您可以通过赋值对其进行修改。

于 2013-04-06T05:45:44.680 回答
0

试试这个,将你的数组分配到一个变量中,假设 $arr。

foreach($arr as $item=>$val)
{   
    $arr[$item]['price']=number_format(str_replace('$','',$val['price']),2);
}

 print_r($arr); // return your desire array format.
于 2013-04-06T05:46:21.790 回答
0

只需遍历数组,修剪任何 $ 符号,并格式化小数:

foreach ($array as $item => $data) {

    // remove $
    $array[$item]['price'] = ltrim($array[$item]['price'], '$');

    // format decimals
    $array[$item]['price'] = number_format($array[$item]['price'], 2, '.', '');

}
于 2013-04-06T05:48:36.337 回答
0

可能这是你想要的:

$result = array (
  1 => 
  array (
    'name' => 'Product 1',
    'price' => '5',
  ),
  2 => 
  array (
    'name' => 'Product 2',
    'price' => '$10',
  ),
  3 => 
  array (
    'name' => 'Product 3',
    'price' => '$50',
  ),
  4 => 
  array (
    'name' => 'Product 4',
    'price' => '20',
  ),
);

$output = array();
foreach($result as $key => $value) {
   $output[$key]['name'] = $value['name'];

   //remove all characters except number and dot.. This will help to remove if instead of $ any other money format comes.
   $new_price = preg_replace("/[^0-9.]/", "", $value['price']);

   $new_price = number_format($new_price, 2, '.', '');
   $output[$key]['price'] = $new_price; 

}

print_R($output);

希望这可以帮助你:)

于 2013-04-06T05:52:55.730 回答
0

这可以满足您的要求:

    $arrayhelp = array (
      1 => 
      array (
        'name' => 'Product 1',
        'price' => '5',
      ),
      2 => 
      array (
        'name' => 'Product 2',
        'price' => '$10',
      ),
      3 => 
      array (
        'name' => 'Product 3',
        'price' => '$50',
      ),
      4 => 
      array (
        'name' => 'Product 4',
        'price' => '20',
      ),
    );

^ 你的数组 V 代码

    foreach ($arrayhelp as &$row):

        $row['price'] = number_format(str_replace('$','',$row['price']),2,'.','');
    endforeach;

    print_r($arrayhelp);

如果你想以千分隔:

    foreach ($arrayhelp as &$row):

        $row['price'] = number_format(str_replace('$','',$row['price']),2,'.',',');
    endforeach;

    print_r($arrayhelp);
于 2013-04-06T05:54:18.100 回答