1

Is there any PHP function to make an array if we have a string and know array count?

example:

$sample = 'required|numeric|between:0,99.99';
$count = 3;


$validation_arr = ['required|numeric|between:0,99.99','required|numeric|between:0,99.99','required|numeric|between:0,99.99'];
2
  • 11
    $validation_arr = array_fill(0, $count, $sample) Commented Apr 4, 2019 at 4:42
  • Worked like a charm. Thanks. Commented Apr 4, 2019 at 4:47

1 Answer 1

1

Yes, there is a predefined array function namely as array_fill which is used to fill given array keys. Below is the example of array_fill and an answer to your question.

    $count = 3;
    $a = array_fill(0,$count, 'required|numeric|between:0,99.99');
    print_r($a);
    //Output Array
    (
       [0] => required|numeric|between:0,99.99
       [1] => required|numeric|between:0,99.99
       [2] => required|numeric|between:0,99.99
    )
Sign up to request clarification or add additional context in comments.

Comments

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.