AnaTeixeira Posted October 14, 2013 Share Posted October 14, 2013 Hi everyone, i need some help. I have an array ($array) - Array ( [0] => 1374410400 [1] => 1374394500 [2] => 1374384000 [3] => 1374304800 [4] => 1374291900 ). And the operation that will be used in array is defined by the user. The operation could be array_sum, count and end. I want to "merge" this two (array + operation), like this $operation.'('.$array.')'. When I echo this $operation.'('.$array.')'. only appears count(Array). But when i write "count($res)" appears the result. Anyone knows the answer? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
NiTx Posted October 14, 2013 Share Posted October 14, 2013 (edited) You can't echo operations, You need to use if or else statements. $operation = $_POST['SelectedOperation'] if($operation == 'count') { echo count($array); } elseif ($operation == 'sum') { echo array_sum($array); } You can have as many elseif statements as you require. Edited October 14, 2013 by NiTx Quote Link to comment Share on other sites More sharing options...
.josh Posted October 14, 2013 Share Posted October 14, 2013 I suppose this sounds like what you want to do? <form action='' method='post'> <input type='text' name='av[]' /><br/> <input type='text' name='av[]' /><br/> <input type='text' name='av[]' /><br/> <input type='text' name='av[]' /><br/> <input type='text' name='av[]' /><br/> <input type='submit' name='operation' value='array_sum' /> <input type='submit' name='operation' value='count' /> <input type='submit' name='operation' value='end' /> </form> <?php if ($_POST) { $array = array_filter($_POST['av']); // filter out empty values $func = $_POST['operation']; if ( function_exists($func) ) echo call_user_func($func,&$array); } ?> Quote Link to comment 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.