我检查过几个类似的问题,例如this,this和this但仍然不清楚,我是否可以通过深层嵌套数组中的操作获得稳定的值,而不是调用功能还是分配变量?
以下示例,根据$arr['row']
插入$s
的第一个位置:
$s = true; //or false;
$arr = [
'key1'=>'val1',
'key2'=>'val2',
'row'=>[
function($s){
if($s) return array('x'=>'y',...);
else return null;
},
[
'row2a'=>'val2a',
'row2b'=>'val2b',
],
[
'row3a'=>'val3a',
'row3b'=>'val3b',
],
],
];
// Output:
Array(
...
[row] => Array
(
[0] => Closure Object
(
[parameter] => Array
(
[$s] =>
)
)
[1] => Array
(
[row2a] => val2a
[row2b] => val2b
)
...
Closure Object
中的array('x'=>'y',...)
而不是$arr['row'][0]
。或者它无法通过数组内的操作获取值,但调用函数或通过变量传递?谢谢。
答案 0 :(得分:1)
如果这是您所需要的,您可以尝试这种方法:
$s = 1;
$value = call_user_func(function($s) { return $s; }, $s);
var_dump($value);
它会产生:
int(1)
答案 1 :(得分:0)
Try below code
$s=true;
function abc($flag) {
if ($flag):
$array["x"]="x";
$array["y"]="y";
return $array;
else:
return null;
endif;
}
$arr = [
'key1' => 'val1',
'key2' => 'val2',
'row' => [
$resultset = abc($s),
[
'row2a' => 'val2a',
'row2b' => 'val2b',
],
[
'row3a' => 'val3a',
'row3b' => 'val3b',
],
],
];
print_r($arr);
exit;
output
Array
(
[key1] => val1
[key2] => val2
[row] => Array
(
[0] => Array
(
[x] => x
[y] => y
)
[1] => Array
(
[row2a] => val2a
[row2b] => val2b
)
[2] => Array
(
[row3a] => val3a
[row3b] => val3b
)
)
)