0

suppose I have a function like

function crear($first, $second, $third, $fourth, $fifth, $sixth){
    $sixth= ($sixth > 0 ? "<span class='required'>*</span>" : "");
    if($fourth=='input'){
        echo "\t   <div class='field-box ".$third."' id='".$first."_field_box'>  \n";
        echo "\t\t <div class='display-as-box' id='".$first."_display_as_box'>".$second."  ".$sixth.":</div>  \n";
        echo "\t\t <div class='input-box' id='".$first."_input_box'> \n";
        echo "\t\t <input id='field-".$first."' name='".$first."' type='text' maxlength='".$fifth."' /> </div><div class='clear'></div>  \n";
        echo "\t   </div>\n";
    }
}

And I am calling it several times:

crear('title1', 'Title 1','odd',  'input', '50', 0 );
crear('title2', 'Title 2','even', 'input', '50', 0 );
crear('title3', 'Title 3','odd',  'input', '30', 1 );
crear('title4', 'Title 4','even', 'input', '50', 0 );
crear('title5', 'Title 5','odd',  'select', '19', 1 );
crear('title6', 'Title 6','even', 'select', '19', 0 );

How could I make only one call to this function passing all this data.

I was thinking to make an array but I woul have to modify the function, What would be the best way... The only one I easily can suppose is odd and even field, the others got to be variables.

1
  • 2
    calling crear() several times, but defining fill()? If you don't want to modify the function itself, then put your data in an array, loop over the array, and call the function within the loop. Commented Mar 25, 2013 at 16:12

1 Answer 1

4

Use the call_user_func_array() function. This allows you to pass an array into a function that would normally accept just a list of parameters.

So let's say your array looks like this: (based on the data in the question)

$input = array(
    array('title1', 'Title 1','odd',  'input', '50', 0 ),
    array('title2', 'Title 2','even', 'input', '50', 0 ),
    array('title3', 'Title 3','odd',  'input', '30', 1 ),
    array('title4', 'Title 4','even', 'input', '50', 0 ),
    array('title5', 'Title 5','odd',  'select', '19', 1 ),
    array('title6', 'Title 6','even', 'select', '19', 0 ),
);

you can use call_user_func_array() to pass the data into your function like this:

foreach($input as $data) {
    call_user_func_array('crear', $data);
}

You can find out more about call_user_func_array() in the PHP manual: http://php.net/manual/en/function.call-user-func-array.php

Sign up to request clarification or add additional context in comments.

3 Comments

+1 Nice, I was going to suggest creating another function to iterate through the array and call crear();
You might want to add the name of the func being called as first param ;)
@Crisp - heh, yep. :) good spot. Typing faster than thinking again ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.