Ad

What To Do Range() Php Function Return Associate Array Instead Nummeric

- 1 answer

I want to create a select box like this :

<select class="form-control" name="quantity">
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
   <option value="4">4</option>
   <option value="5">5</option>
   <option value="6">6</option>
   <option value="7">7</option>
   <option value="8">8</option>
   <option value="9">9</option>
   <option value="10">10</option>
</select>

For that I write this laravel blade code :

{!! Form::select('quantity', range(1,10), null , ['class'=>'form-control']) !!}

But this return a select with zero-based options like this :

<select class="form-control" name="quantity">
   <option value="0">1</option>
   <option value="1">2</option>
   <option value="2">3</option>
   <option value="3">4</option>
   <option value="4">5</option>
   <option value="5">6</option>
   <option value="6">7</option>
   <option value="7">8</option>
   <option value="8">9</option>
   <option value="9">10</option>
</select>

This is because range() php function always return a numeric Array .is there a way to force it to return associative Array?

Update :
According to @Shailesh Katarmal comment , simply I should to use selectRange method.

Ad

Answer

please try the following code

{!! Form::select('quantity', array_combine(range(1, count(range(1,10))), array_values(range(1,10)) ), null , ['class'=>'form-control']) !!}
Ad
source: stackoverflow.com
Ad