あるアルゴリズム

1687 ワード

<?php
function ExplodeLines($text, $columnNames)
{
$arr = array();
$r1 = explode("
", $text); unset($r1[0]); unset($r1[count($r1)]); foreach ($r1 as $v1) { $v1 = explode(',', $v1); array_push($arr, $v1); } foreach ($arr as &$v) { $v = array_combine($columnNames, $v); } return $arr; } $text = " Apple,20,red Pear,10,yellow "; $columnNames = array('Fruit', 'Number', 'Color'); $result = ExplodeLines($text, $columnNames); var_export($result); /* array( array('Fruit'=>'Apple', 'Number'=>'20', 'Color'=>'red'), array('Fruit'=>'Pear', 'Number'=>'10', 'Color'=>'yellow'), ) */
割引付きの返品アルゴリズム
function CalcRefundAmount($orderItems, $discountAmount, $refundItems)
{
	$arr = $arrAmout = array();
	foreach ($orderItems as $k=>$v)
	{
		$num = array_product($v);
		array_push($arr, $num);
	}
	$count = array_sum($arr);
	$realpay = $count - $discountAmount;
	foreach ($refundItems as $k=>$v)
	{
		$refundAmout = ($orderItems[$k]['price'] * $v) * ($realpay / $count);
		array_push($arrAmout, round($refundAmout, 2));
	}
		
	return array_sum($arrAmout);
}

//(5.0 * 1) * (20.0/30.0) = 3.33  (   2   )
$orderItems = array(
	'ItemA'=>array('price'=>5.0, 'quantity'=>2),
	'ItemB'=>array('price'=>20.0, 'quantity'=>1),
);
$refundItems = array('ItemA'=>1);
$discountAmount=10;

$result = CalcRefundAmount($orderItems, $discountAmount, $refundItems);
echo $result;