[英]How cast or convert array value string to boolean in php
我正在使用Laravel ACL
。在每個角色中都有一組權限。 我使用復選框表示所有權限。 當我提交表單時,它會返回一個這樣的權限數組。
{
read: "true",
create: "true",
delete: "false",
update: "false"
}
數組值顯示為double quotes
字符串,但我希望將其作為boolean
如何將數組值字符串轉換為布爾值。
@foreach($role->permissions as $key=>$value)
<td>
<input type="hidden" name="permission[{{$key}}]" class="role" value="false" {{ $value==0 ? 'checked' : '' }}>
<input type="checkbox" name="permission[{{$key}}]" class="role" value="true" {{ $value==1 ? 'checked' : '' }}>
</td>
@endforeach
我想這樣排列值。
{
read: true,
create: true,
delete: false,
update: false
}
HTML雙引號值。 如果您的值始終為“ true”或“ false”,則可以專門檢查“ true”。
// change your json into an array
$values = json_decode($yourValues, true) ;
// loop through and check for "true"
foreach($values as $key=>$value) {
$values[$key] = ($value == "true") ;
}
// if you need it as json again
$yourValues = json_encode($values) ;
對你來說
@foreach($role->permissions as $key=>$value)
<td>
<input type="hidden" name="permission[{{$key}}]" class="role" value="false" {{ (boolean)$value ? 'checked' : '' }}>
<input type="checkbox" name="permission[{{$key}}]" class="role" value="true" {{ (boolean)$value? 'checked' : '' }}>
</td>
@endforeach
這樣嘗試
####編輯####
$data=$_POST[];
foreach($data as $record){
$record=(boolean)$record;
//now $record is boolean
}
這樣嘗試
@foreach($role->permissions as $key=>$value)
<td>
<input type="hidden" name="permission[{{$key}}]" class="role" value=false {{ $value=='false' ? 'checked' : '' }}>
<input type="checkbox" name="permission[{{$key}}]" class="role" value=true {{ $value=='true' ? 'checked' : '' }}>
</td>
@endforeach
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.