Jump to content

Need Help tp simplify a function.


anand0412
Go to solution Solved by mac_gyver,

Recommended Posts

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.

 

Link to comment
Share on other sites

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 by mac_gyver
  • Like 1
Link to comment
Share on other sites

  • Solution

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 by mac_gyver
  • Like 1
Link to comment
Share on other sites

 

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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.