giraffemedia Posted August 28, 2008 Share Posted August 28, 2008 Does anyone know of an alternative function to array_product? I've uploaded my site to a live server and it's only running php 4.4.8 and not 5 that array_product needs. James Quote Link to comment https://forums.phpfreaks.com/topic/121673-array_product-alternative/ Share on other sites More sharing options...
JasonLewis Posted August 28, 2008 Share Posted August 28, 2008 Ummm, not sure about function wise. Make your own. function array_product($arr){ $product = 1; //start at 1 foreach($arr as $values){ $product *= $values; } return $product; } $getMultiply = array_product(array(4,6,1,7)); echo $getMultiply; //Will output 168 Quote Link to comment https://forums.phpfreaks.com/topic/121673-array_product-alternative/#findComment-627687 Share on other sites More sharing options...
giraffemedia Posted August 28, 2008 Author Share Posted August 28, 2008 I get the following error... Fatal error: Cannot redeclare array_product() (previously declared in /Applications/MAMP/htdocs/database/database_files/tools/page_space_sold.php:118) in /Applications/MAMP/htdocs/database/database_files/tools/page_space_sold.php on line 118 I haven't declared it before then so why is this happening? Quote Link to comment https://forums.phpfreaks.com/topic/121673-array_product-alternative/#findComment-627690 Share on other sites More sharing options...
JasonLewis Posted August 28, 2008 Share Posted August 28, 2008 Because it's already a function. Is that on the PHP4 server. If it's on the PHP4 server it shouldn't be throwing you the error. If you test it on a PHP5 server it will throw you that error. Call the function something else. That will fix it. array_multiple or something. Quote Link to comment https://forums.phpfreaks.com/topic/121673-array_product-alternative/#findComment-627693 Share on other sites More sharing options...
giraffemedia Posted August 28, 2008 Author Share Posted August 28, 2008 The function doesn't output anything ProjectFear. What do I have to do with it? Quote Link to comment https://forums.phpfreaks.com/topic/121673-array_product-alternative/#findComment-627842 Share on other sites More sharing options...
obsidian Posted August 28, 2008 Share Posted August 28, 2008 He wrote it to return a value, so you need to echo the result. Quote Link to comment https://forums.phpfreaks.com/topic/121673-array_product-alternative/#findComment-627845 Share on other sites More sharing options...
giraffemedia Posted August 28, 2008 Author Share Posted August 28, 2008 Excuse my ignorance but how do I do that obsidian? I've not really used functions before. James Quote Link to comment https://forums.phpfreaks.com/topic/121673-array_product-alternative/#findComment-627850 Share on other sites More sharing options...
akitchin Posted August 28, 2008 Share Posted August 28, 2008 $array = array(1, 2, 3, 4, 5); $product = array_product($array); echo $product; or simply: echo array_product($array); Quote Link to comment https://forums.phpfreaks.com/topic/121673-array_product-alternative/#findComment-627851 Share on other sites More sharing options...
giraffemedia Posted August 28, 2008 Author Share Posted August 28, 2008 $array = array(1, 2, 3, 4, 5); $product = array_product($array); echo $product; or simply: echo array_product($array); but I can't use array_product. That's why i'm looking at a different way akitchin. Quote Link to comment https://forums.phpfreaks.com/topic/121673-array_product-alternative/#findComment-627852 Share on other sites More sharing options...
akitchin Posted August 28, 2008 Share Posted August 28, 2008 change the function name to whatever you named the function that ken gave you earlier in the thread - my post is simply to show you how to echo what the function returns. Quote Link to comment https://forums.phpfreaks.com/topic/121673-array_product-alternative/#findComment-627854 Share on other sites More sharing options...
discomatt Posted August 28, 2008 Share Posted August 28, 2008 Loffles. if ( !function_exists('array_product') ) { function array_product($arr) { $product = 1; //start at 1 foreach($arr as $values) $product *= $values; return $product; } } Quote Link to comment https://forums.phpfreaks.com/topic/121673-array_product-alternative/#findComment-627855 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.