Javascript 这个函数的Php等价物
我有这个javascript函数,我需要将其转换为php函数Javascript 这个函数的Php等价物,javascript,php,Javascript,Php,我有这个javascript函数,我需要将其转换为php函数 function vwap(p) { if (p.length === 1 && Array.isArray(p[0])) p = p[0] if (!p.length) return 0 // formula: sum(num shares * share price)/(total shares) return p.reduce((s,x) => s + (x[0]*x[1]), 0) / p
function vwap(p)
{
if (p.length === 1 && Array.isArray(p[0])) p = p[0]
if (!p.length) return 0
// formula: sum(num shares * share price)/(total shares)
return p.reduce((s,x) => s + (x[0]*x[1]), 0) / p.reduce((s,x) => s + x[0], 0) || 0;
}
到目前为止,我在下面试过,但我被打动了
function vwap($p) {
if (empty($p)) {
return 0;
for ($p as $s) {
//unable to understand this part in js
}
}
}
出于测试目的vwap([[5,10],[13,8.5],[10,11]])
必须返回9.66
function vwap($p)
{
if (count($p) === 1 && is_array($p[0])) {
$p = $p[0];
}
if (!count($p)) {
return 0;
}
return array_reduce($p, function ($s, $x) {
return $s + ($x[0] * $x[1]);
}, 0) / array_reduce($p, function ($s, $x) {
return $s + $x[0];
}, 0);
}
1:1 php端口。我对php不太了解,但我可以解释JS及其功能,特别是reduce部分。不确定这会有多大帮助,但希望它能让您更好地理解JS在做什么,并在您自己的PHP代码中实现它
// formula: sum(num shares * share price)/(total shares)
return p.reduce((s,x)=>s+(x[0]*x[1]),0)/p.reduce((s,x)=>s+x[0],0)||0;
Array.reduce
是一个内置的递归函数。在JS中,最简单的重写方法是:
function reduce(iterator, accumulator, reduceFunc){
for(var i of iterator){
accumulator = reduceFunc(accumulator, i)
}
return accumulator
}
这意味着p
是迭代器,0
是累加器,(s,x)=>s+(x[0]*x[1])
以及(s,x)=>s+x[0]
是reduce函数。因此reduce
遍历p
的每个元素,并调用提供的函数作为累加器的函数(始终是要reduce的最后一个参数,在本例中为0
)和p
的当前元素,最后返回该函数的结果,然后与p
的下一个元素组合,或者在迭代完成后返回
我可以重写
return p.reduce((s,x) => s + (x[0]*x[1]), 0) / p.reduce((s,x) => s + x[0], 0) || 0
作为更详细的代码:
// this would be the leftmost reduce function:
function reduceNumerator(accumulator, currentItem){
return accumulator + (currentItem[0] * currentItem[1])
}
// this would be the rightmost reduce function:
function reduceDenominator(accumulator, currentItem){
return accumulator + currentItem[0]
}
因此,要使用这些更详细的函数重写整个过程:
function vwap(p){
if (p.length === 1 && Array.isArray(p[0])) p = p[0]
if (!p.length) return 0
var numerator = 0
var denominator = 0
for(var i of p){
numerator = reduceNumerator(numerator, i)
denominator = reduceDenominator(denominator, i)
}
return (numerator / denominator) || 0
}
如果您想轻松验证此代码是否从您提供的输入输出正确的结果,请使用以下工具:使用
array\u reduce
尽可能接近原始结果:
函数vwap1(数组$p):浮点
{
if(count($p)==1&&is_数组($p[0])){
$p=$p[0];
}
如果(计数($p)==0){
返回0;
}
$top=array\u reduce($p,函数(float$s,array$x):float{
返回$s+($x[0]*$x[1]);
}, 0);
$bottom=array\u reduce($p,函数(float$s,array$x):float{
返回$s+x[0];
}, 0);
如果($bottom==0){
返回0;
}
返回$top/$bottom;
}
将这两种方法结合在一起:
函数vwap2(数组$p):浮点
{
if(count($p)==1&&is_数组($p[0])){
$p=$p[0];
}
如果(计数($p)==0){
返回0;
}
$top=0;
$bottom=0;
foreach($p为$x){
$top+=$x[0]*$x[1];
$bottom+=$x[0];
}
如果($bottom==0){
返回0;
}
返回$top/$bottom;
}
PHP中的预期代码:
function vwap($p) {
if (count($p) == 1 && is_array($p[0])) {
$p = $p[0];
}
if (empty($p)) {
return 0;
}
// (s,x) => s + (x[0]*x[1]), 0
$r1 = array_reduce($p, function ($s, $x) {
return $s + $x[0] * $x[1];
}, 0);
// (s,x) => s + x[0], 0
$r2 = array_reduce($p, function ($s, $x) {
return $s + $x[0];
}, 0);
if (!$r1 || !$r2) {
return 0;
}
return $r1 / $r2;
}
$result = vwap([[5, 10], [13, 8.5], [10, 11]]);
var_dump($result);
产出:
float(9.6607142857143)
您在将JS脚本的哪一部分转换为for循环时遇到问题?不确定JS版本是否有问题,但这不会返回
9.66
@Graciewilliams my bad,我忘记了函数中的返回。。现在应该纠正了。