anand0412 Posted July 29, 2022 Share Posted July 29, 2022 Hi all. I have a function like this function SpecificGifts ( $benfield , $propcat , $proptype , $proppost , $titledet , $MOGM , $FSG , $VDesc , $Vreg , $Jdesc , $ODesc ) { //code ; } I then call the function like this echo SpecificGifts ( $form_data['field'][77] , $form_data['field'][80] , $form_data['field'][65] , $form_data['field'][66] , $form_data['field'][67] , $form_data['field'][83] , $form_data['field'][74] , $form_data['field'][87] , $form_data['field'][88] , $form_data['field'][90] , $form_data['field'][92]); Is there a way i can write this more elegant and simpler format? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/315102-need-help-tp-simplify-a-function/ Share on other sites More sharing options...
mac_gyver Posted July 29, 2022 Share Posted July 29, 2022 (edited) how are those array indexes 77, 80, ... determined, is that all the possible index values or just a sub-set of them, and what does the function code do with the input parameters? ideally, you would supply one array input parameter, with elements that are the set of input values. just based on the posted information, I would make an array of the index values, then loop to build the one array of inputs, then call the function with that array. Edited July 29, 2022 by mac_gyver 1 Quote Link to comment https://forums.phpfreaks.com/topic/315102-need-help-tp-simplify-a-function/#findComment-1598709 Share on other sites More sharing options...
Solution mac_gyver Posted July 29, 2022 Solution Share Posted July 29, 2022 (edited) assuming the function code already exists and you don't want to change it, you can use php's splat ... operator to call the function with an array of values that will get expanded into the individual input parameters. $gift_fields = [77, 80, 65, 66, 67, 83, 74, 87, 88, 90, 92]; $params = []; foreach($gift_fields as $field) { $params[] = $form_data['field'][$field]; } echo SpecificGifts(...$params); if you rewrite the function code to accept and operate on elements of a single array input parameter, you would change the last line to - echo SpecificGifts($params); Edited July 29, 2022 by mac_gyver 1 Quote Link to comment https://forums.phpfreaks.com/topic/315102-need-help-tp-simplify-a-function/#findComment-1598710 Share on other sites More sharing options...
anand0412 Posted July 29, 2022 Author Share Posted July 29, 2022 23 minutes ago, mac_gyver said: assuming the function code already exists and you don't want to change it, you can use php's splat ... operator to call the function with an array of values that will get expanded into the individual input parameters. $gift_fields = [77, 80, 65, 66, 67, 83, 74, 87, 88, 90, 92]; $params = []; foreach($gift_fields as $field) { $params[] = $form_data['field'][$field]; } echo SpecificGifts(...$params); if you rewrite the function code to accept and operate on elements of a single array input parameter, you would change the last line to - echo SpecificGifts($params); Hi. Thanks for the suggestion and effort. However, I tried it and it threw an error. Maybe if I give my full function it would be more helpful :- function SpecificGifts ( $benfield , $propcat , $proptype , $proppost , $titledet , $MOGM , $FSG , $VDesc , $Vreg , $Jdesc , $ODesc ) { if ( is_array( $benfield ) ){ echo '<li>'; if ( count( $benfield ) == 1) { $beneficiaries = (implode(', ', $benfield)) . '.'; } elseif (count( $benfield ) > 1 ) { $beneficiaries = (implode(', ', $benfield)) ; $beneficiaries = preg_replace("/,([^,]+)$/", " and $1", $beneficiaries) . ' equally.' ; } Switch ($proptype) { case 'Immovable Property (Land / House)': return 'My ' . $propcat . ' bearing postal address ' . $proppost . ' held under '. $titledet . ' to ' . $beneficiaries ; break; case 'Money' : if ($MOGM = 'All My Money') { return 'All my money to ' . $beneficiaries ; } elseif ($MOGM = 'A Fixed Sum of Money') { return '<li> A sum of ' . $FSG . ' to ' . $beneficiaries ; } case 'Vehicle' : return 'My ' . $VDesc . ' bearing registration number ' . $Vreg . ' to ' . $beneficiaries ; break; case 'Jewellery' : return $Jdesc . ' to ' . $beneficiaries ; break; case 'Others' : return $ODesc . ' to ' . $beneficiaries ; break; } echo '</li>'; } } $gift_fields = [77, 80, 65, 66, 67, 83, 74, 87, 88, 90, 92]; $params = []; foreach($gift_fields as $field){ $params[] = $form_data['field'][$field]; } echo SpecificGifts($params); data field is the data from gravity form. Quote Link to comment https://forums.phpfreaks.com/topic/315102-need-help-tp-simplify-a-function/#findComment-1598711 Share on other sites More sharing options...
mac_gyver Posted July 29, 2022 Share Posted July 29, 2022 1 hour ago, mac_gyver said: assuming the function code already exists and you don't want to change it, you can use php's splat ... operator so, if you are using the original function code, why did you remove the use of the ... splat operator? 1 Quote Link to comment https://forums.phpfreaks.com/topic/315102-need-help-tp-simplify-a-function/#findComment-1598712 Share on other sites More sharing options...
anand0412 Posted July 29, 2022 Author Share Posted July 29, 2022 16 minutes ago, mac_gyver said: so, if you are using the original function code, why did you remove the use of the ... splat operator? Thank you. I didn't know what a splat operator was. Your solution is perfect. Quote Link to comment https://forums.phpfreaks.com/topic/315102-need-help-tp-simplify-a-function/#findComment-1598713 Share on other sites More sharing options...
anand0412 Posted July 29, 2022 Author Share Posted July 29, 2022 9 hours ago, mac_gyver said: assuming the function code already exists and you don't want to change it, you can use php's splat ... operator to call the function with an array of values that will get expanded into the individual input parameters. $gift_fields = [77, 80, 65, 66, 67, 83, 74, 87, 88, 90, 92]; $params = []; foreach($gift_fields as $field) { $params[] = $form_data['field'][$field]; } echo SpecificGifts(...$params); if you rewrite the function code to accept and operate on elements of a single array input parameter, you would change the last line to - echo SpecificGifts($params); Hi @mac_gyver, I'm sorry if this is a noob question. I'm trying to understand this code. What is the purpose of the line 2 code - $params = []; what happens if I remove line 2? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/315102-need-help-tp-simplify-a-function/#findComment-1598732 Share on other sites More sharing options...
mac_gyver Posted July 29, 2022 Share Posted July 29, 2022 it initializes the variable to an empty array, in case the same variable was previously used in the current code and already has something in it that doesn't have anything to do with the current usage. Quote Link to comment https://forums.phpfreaks.com/topic/315102-need-help-tp-simplify-a-function/#findComment-1598735 Share on other sites More sharing options...
anand0412 Posted July 29, 2022 Author Share Posted July 29, 2022 27 minutes ago, mac_gyver said: it initializes the variable to an empty array, in case the same variable was previously used in the current code and already has something in it that doesn't have anything to do with the current usage. Thank you for the clarification. I truly appreciate your help here. Quote Link to comment https://forums.phpfreaks.com/topic/315102-need-help-tp-simplify-a-function/#findComment-1598741 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.