<?php
function mkProductList($array) {
// $tmpVal get product of all elements
$tmpVal = 1;
// Count zeros in array
$cnt = count(array_keys($array, 0));
// If there are two or more zeros, then product of all elements will be zero
// No make sense to do
// Fill array with zeros and return it
if ($cnt > 1) {
$array = array_fill_keys(array_keys($array), 0);
return $array;
}
// If there are one or no zeros, then get product of all elements, exclude zero,
// cause multiply by zero equal zero
foreach($array as $value) {
if ($value != 0) {
$tmpVal *= $value;
}
}
// If there are one zero, then product of all elements will be zero
// exclude zero position in input array, it will be product of all elements
if ($cnt == 1) {
$array = array_fill_keys(array_keys($array), 0);
$array[array_search(0, $array)] = $tmpVal;
return $array;
}
// If there are no zeros, then we replace velues in input array
// Product of all elements we devide by current element value
// Result is product of all elements, exclude current element value
foreach($array as $key => $value) {
$array[$key] = $tmpVal / $value;
}
return $array;
}
// tests
var_dump(producePatStr(array(1,2,3,4)));
var_dump(producePatStr(array()));
var_dump(producePatStr(array(-0.5, 100, 20)));
?>