php - Reformat value in array -
i have array:
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', ), ) i need loop through array, reformat prices in decimal format. example 10 10.00 , 50 50.00. need make sure $ removed if exists user submitting value of $50
after these values replaced. need array looks in value of $result should like:
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', ), ) thanks, help!
may want :
$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 $key => $value) { $output[$key]['name'] = $value['name']; //remove characters except number , dot.. remove if instead of $ 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); hope helps :)
Comments
Post a Comment