3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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))); ?>
based on ivCMS
Output for 7.0.0 - 7.2.0
Fatal error: Uncaught Error: Call to undefined function producePatStr() in /in/QKK25:46 Stack trace: #0 {main} thrown in /in/QKK25 on line 46
Process exited with code 255.
Output for 5.3.20 - 5.6.28
Fatal error: Call to undefined function producePatStr() in /in/QKK25 on line 46
Process exited with code 255.