我卡在一点上我有两个数组,一个包含读数据,另一个包含写,如下所示
读取数组
Array
(
[0] => master/city
[1] => master/national_holiday
[2] => master/operator_comments
[3] => master/sensors
)
写数组
Array
(
[0] => master/city
[1] => master/national_holiday
)
我想结合这两个数组。我想要如下
Array
(
['master/city'] => 'read/write'
['master/national_holiday'] => 'read/write'
['master/operator_comments'] => 'read'
['master/sensors'] => 'read'
)
这样可能吗?
答案 0 :(得分:2)
你可以这样做:
这是DEMO
$final = [];
array_walk($read,function($v,$k) use (&$final,&$write){
if(array_search($v,$write) > -1){
$final[$v] = 'read/write';
}
else{
$final[$v] = 'read';
}
});
print_r($final);
<强>输出强>
Array
(
[master/city] => read/write
[master/national_holiday] => read/write
[master/operator_comments] => read
[master/sensors] => read
)
修改强>
根据与Dhanashri
:
$read_flipped = array_flip($read);
$read_new = array_fill_keys(array_keys($read_flipped), 'read');
$write_flipped = array_flip($write);
$write_new = array_fill_keys(array_keys($write_flipped), 'write');
$final = [];
$arr3 = array_merge_recursive($read_new,$write_new);
array_walk($arr3,function($v,$k) use (&$final) {
if(is_array($v)){
$final[$k] = 'read/write';
}
else{
$final[$k] = $v;
}
});
print_r($final);
答案 1 :(得分:1)
var="rule:access=allow,OTHER=0,SOMETHING=1"
O / P
$read = array
(
"master/city",
"master/national_holiday",
"master/operator_comments",
"master/sensors",
);
$write = array(
"master/city",
"master/national_holiday",
);
$new_array = array_merge($read,$write);
$your_array = array();
foreach($new_array as $key => $value ){
$your_array[$value] = (array_key_exists($value,$your_array)) ? "read/write" : "read";
}
var_dump($your_array);
你也可以使用
做同样的事情array (size=4)
'master/city' => string 'read/write' (length=10)
'master/national_holiday' => string 'read/write' (length=10)
'master/operator_comments' => string 'read' (length=4)
'master/sensors' => string 'read' (length=4)
答案 2 :(得分:1)
// we need somed
$read = array('master/city',
'master/national_holiday',
'master/operator_comments',
'master/sensors');
// Write variable
$write = array('master/city','master/national_holiday');
$newarray = array();
foreach($read as $key => $value)
{
// check value in another array
if(in_array($value,$write))
{
$access = 'read/write';
}
else
{
$access = 'read';
}
$newarray[$value] = $access;
}
print_r($newarray);