0

I have this piece oh code:

<?php
    
$levels = array( "0" => "Super User", "1" => "Administrator", "10" => "10", "20" => 20, "30" => "30", "40" => "40", "50" => "50", "99" => "99 News homepage" );

?>

<select name="level" id="modify_modal_level">
    @foreach( $levels as $key => $val )
            <option value="{{ $key }}" <?php echo $selected==$key ? 'selected="selected"': ""?>>{{ $val }}</option>
    @endforeach
</select>

Why the $key inside foreach return the INDEX of array? example:

@foreach( $levels as $key => $val )
   {{ $key }},
@endforeach

prints out: 1,2,3,4,5,6,7,8,

instead of: '0','1','10','20','30','40','50','99'

but the foreach loop in the $key variable MUST return the key value of the associative array, not the key index!

my select results:

<select name="level" id="modify_modal_level">
<option value="1">Super User</option>
<option value="2">Administrator</option>
<option value="3">10</option>
...
<option value="8">99 News homepage</option>
</select>

instead of:

<select name="level" id="modify_modal_level">
<option value="0">Super User</option>
<option value="1">Administrator</option>
<option value="10">10</option>
...
<option value="99">99 News homepage</option>
</select>

Thanks!!

5
  • 1
    It's difficult to understand what you are getting and what you are wanting. Can you clarify? Commented Jan 20, 2016 at 17:51
  • 2
    I just executed your provided code and received the 0, 1, 10 index keys as expected. Are you sure your $levels variable contains the data you think it contains? Commented Jan 20, 2016 at 18:08
  • If you are wanting what you have under "example" then simply remove the keys in your $levels array. Commented Jan 20, 2016 at 18:12
  • 1
    @PatrickStephan it looks like they want what's under "instead of"...I think. However, that's what they would get with the blade provided...so now I'm not so sure. Commented Jan 20, 2016 at 18:14
  • i have tried to clarify updating the main post!! Commented Jan 20, 2016 at 20:47

1 Answer 1

1

Your code will work as you have it now; are you certain that $levels is correctly generated as you're showing it here?

Controller

$levels = [
    0 => "Super User",
    1 => "Administrator",
    10 => 10,
    20 => 20,
    30 => 30,
    40 => 40,
    50 => 50,
    99 => "99 News homepage",
];

return view('yourview', [
    'levels' => $levels,
    'selected' => '99'
]);

yourview.blade.php

<select name="level" id="modify_modal_level">
    @foreach ($levels as $key => $value)
        <option value="{{ $key }}" @if ($selected == $key) selected @endif>{{ $value }}</option>
    @endforeach
    {{-- $key will be 0, 1, 10, 20, 30, 40, 50, 99 --}}
    {{-- $value will be "Super User", "Administrator", 10, 20, 30, 40, 50, "99 News homepage" --}}
</select>

Note that I also updated to use @if Blade syntax since it's much cleaner and easier to read.

When accessing the view, I get this returned:

enter image description here

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

3 Comments

the code is all in a include file, loaded with @include(..). The array is initialized inside the include!!
That should not make a difference. I just used controller because it's how I have my layout defined. If you're loading $levels as the array is shown above, then it should still work. Have you tried var_dump() or dd() on $levels to confirm the index's are correct?
solved!! i have used "normal" php foreach <?php foreach() ?> and all works fine!!

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.