I have this code:
$arrayWithSelectedValues = [[user_id] => 3,[language_id] => 2], [user_id] => 3,[language_id] => 12]]
And I have checkbox :
@foreach($languages as $language)
<fieldset>
<input type="checkbox"
class="icheckbox_square-red"
id="input-15" name="languages[]"
value="{{ $language->id }}">
<label for="input-15">{{ $language->name }}</label>
</fieldset>
@endforeach
How can I mark checkboxes as marked - those that have language_id from the array = $ language-> id?
Use the php function in_array() to check if the $language_id exists in the array of $languages
@foreach($languages as $language)
<fieldset>
<input type="checkbox"
class="icheckbox_square-red"
id="input-15" name="languages[]"
value="{{ $language->id }}"
@if(in_array($language_id, $languages))
checked
@endif
>
<label for="input-15">{{ $language->name }}</label>
</fieldset>
@endforeach
If $language->id
value you are looking for, and you create an array of checked language ids from $arrayWithSelectedValues
you can do:
<input
type="checkbox"
@if(in_array($language->id, $languages))
checked
@endif
>
You can collected the language IDs like so:
$languages = [];
foreach ($arrayWithSelectedValues as $selectedValues) {
$languages[] = $selectedValues['language_id'];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.