I have this javascript function, I need to convert this to a php function
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;
}
I tried below so far but I am struck
function vwap($p) {
if (empty($p)) {
return 0;
for ($p as $s) {
//unable to understand this part in js
}
}
}
For testing purpose vwap([[5, 10], [13, 8.5], [10, 11]])
must return 9.66