你可以试试
$step = array(
'step_1' => 1,
'step_2' => 1,
'step_3' => 0,
'step_4' => 0,
'step_5' => 0
);
var_dump(__missing($step));
var_dump($step);
输出
string 'step_3' (length=6)
array
'step_1' => int 1
'step_2' => int 1
'step_3' => int 1
'step_4' => int 0
'step_5' => int 0
示例 2
$step = array(
'step_1' => 1,
'step_2' => 1,
'step_3' => 1,
'step_4' => 1,
'step_5' => 1
);
var_dump(__missing($step));
var_dump($step);
输出
string 'step_1' (length=6)
array
'step_1' => int 1
'step_2' => int 0
'step_3' => int 0
'step_4' => int 0
'step_5' => int 0
使用的功能
function __missing(&$array) {
$left = array_filter($array, function ($var) {
return ($var == 1) ? false : true;
});
if (! empty($left)) {
$key = key($left);
$array[$key] = 1;
return $key;
} else {
array_walk($array, function (&$var) {
return $var = 0;
});
$key = key($array);
$array[$key] = 1;
return $key;
}
}