Laravel ForEach from Array, How to get the Key

Viewed 37

So my array looks like this

Now, I want to show them in Views in a foreach. But when I do this,

@foreach($schema_const as $const)
         <option value="{{$const}}">{{$const}} </option>
@endforeach

it get the data from the right (value). How do I loop the data from the left? (the key)

2 Answers

you can get key like this from loop

@foreach($schema_const as $key => $const)
         <option value="{{$key}}">{{$const}} </option>
@endforeach

You can get the key in for loop by

@foreach($schema_const as $key => $const)
    <option value="{{$key}}">{{$const}} </option>
@endforeach

In the above loop, the $key will be key of each element in the Object/Array

Related