Phpforeach loop array and set key
foreach($role as $k=>$v){$arrayRole[$k=$v->role_id]=$v->role_code;}
For example, the key I wrote cannot be set.
Reply to discussion (solution)
Why can't I set it? print_r ($ role); check it out.
Foreach ($ role as $ k => $ v ){
$ ArrayRole [$ v-> role_id] = $ v-> role_code;
}
Foreach ($ role as $ k => $ v ){
$ ArrayRole [$ v-> role_id] = $ v-> role_code;
}
Or
Foreach ($ role as $ k => $ v ){
$ ArrayRole [$ k] [$ v-> role_id] = $ v-> role_code;
}
$k=$v->role_id
Can this be a key?
The output is [user_roles] => Array ([0] => 1 [1] => 3 ).
Of course not. The second dimension must be an object. The role_id and role_code attributes are available.
... Not mine is a one-dimensional array. the front edge is in yii format.
I just want to put an array to [user_roles ].
Foreach ($ role as $ k => $ v ){
$ ArrayRole [$ k = $ v-> role_id] = $ v-> role_code;
}
After I put the written array in this way, I found that the key of the array was not set successfully.
Print_r ($ role );
Post result
Array ([user_name] => sss [user_password] => 123 [user_roles] => Array ([0] => 1 [1] => 3) [user_sex] => male [user_birthday] => 1992-12-23 [user_relation] => 18716439681 [dep_id] => dep_cn [user_address] => xxxxxx [user_allowed] => [user_createtime] => 0 [user_lasttime] => 0 [user_realname] => demo [user_id] =>)
I'm talking about the red area
1. you have an array, not an object. Cannot be accessed using objects
2. does your foreach ($ role as $ k => $ v) {indicate the red part? Apparently not
According to my requirements, it should be the whole data
Foreach ($ role as $ k => $ v ){
If ($ k = 'User _ roles '){....}
}
You 'd better describe the form you plan to process
Foreach ($ role as $ k => $ v) {
$v){ $arrayRole[$v->role_id]=$v->role_code;} ?>
labelEx($model,'user_roles'); ?>
checkBoxList($model,'user_roles',$arrayRole); ?>
error($model,'user_roles'); ?>
This is the code in red.
I just directed to [user_roles] => Array ([0] => 1 [1] => 3) I just want to ask why the key value of my array is not set successfully.
If you don't talk about other things, you may have problems writing them yourself.
$ K is defined in foreach.
Then the new array has $ k = $ v-> role_id. at this time, the $ k of foreach has changed.
What is the array you want?
Foreach ($ role as $ k => $ v) {
$v){ $arrayRole[$v->role_id]=$v->role_code;} ?>
labelEx($model,'user_roles'); ?>
checkBoxList($model,'user_roles',$arrayRole); ?>
error($model,'user_roles'); ?>
This is the code in red.
I just directed to [user_roles] => Array ([0] => 1 [1] => 3) I just want to ask why the key value of my array is not set successfully.
Check whether $ v has the role_id attribute.
foreach($role as $k=>$v){ $arrayRole[$v->role_id]=$v->role_code;}
I would like to ask if there is any problem with my code. there are still no other methods for assigning values in the foreach loop and setting the key value. if you have the ability to be caring, tell me where I went wrong. I would like to thank these young people here.
The array key I want is put in the role id value and the role encoding.
Foreach ($ role as $ k => $ v) {
$v){ $arrayRole[$v->role_id]=$v->role_code;} ?>
labelEx($model,'user_roles'); ?>
checkBoxList($model,'user_roles',$arrayRole); ?>
error($model,'user_roles'); ?>
This is the code in red.
I just directed to [user_roles] => Array ([0] => 1 [1] => 3) I just want to ask why the key value of my array is not set successfully.
Check whether $ v has the role_id attribute.
Role_id has a value
From this foreach alone, there is no problem.
Oh, I'm studying
Foreach is okay. The key is that your array does not have the role_id and role_code elements you mentioned.