我可以使用array_walk_recursive函数来修改某些特定的值,比如这个
array_walk_recursive($arr, function (& $item, $key) {
if($key === 'name') {
$item = 'some value';
}
});
但是我如何修改密钥名称本身,它不能与$ key的引用一起工作。请帮帮我。
$ arr here
$arr = [
0 => [
"id" => 1,
"name" => "departmentName",
"code" => "1",
"orderNumber" => 1,
"enabled" => true,
"createSpace" => false,
"description" => "balabala",
"lft" => 1,
"lvl" => 0,
"rgt" => 4,
"children" => [
0 => [
"id" => 7,
"name" => "departmentName_ProblemHere",
"code" => "7",
"orderNumber" => 7,
"enabled" => true,
"createSpace" => false,
"description" => null,
"lft" => 2,
"lvl" => 1,
"rgt" => 3,
"children" => []
]
]
],
1 => [
"id" => 2,
"name" => "departmentName",
"code" => "1",
"orderNumber" => 2,
"enabled" => true,
"createSpace" => false,
"description" => null,
"lft" => 1,
"lvl" => 0,
"rgt" => 2,
"children" => []
],
2 => [
"id" => 3,
"name" => "departmentName",
"code" => "1",
"orderNumber" => 3,
"enabled" => true,
"createSpace" => false,
"description" => null,
"lft" => 1,
"lvl" => 0,
"rgt" => 2,
"children" => []
],
3 => [
"id" => 4,
"name" => "departmentName",
"code" => "4",
"orderNumber" => 4,
"enabled" => true,
"createSpace" => false,
"description" => null,
"lft" => 1,
"lvl" => 0,
"rgt" => 2,
"children" => []
],
4 => [
"id" => 5,
"name" => "departmentName",
"code" => "5",
"orderNumber" => 1,
"enabled" => true,
"createSpace" => false,
"description" => null,
"lft" => 1,
"lvl" => 0,
"rgt" => 2,
"children" => []
],
5 => [
"id" => 6,
"name" => "departmentName",
"code" => "6",
"orderNumber" => 6,
"enabled" => true,
"createSpace" => false,
"description" => null,
"lft" => 1,
"lvl" => 0,
"rgt" => 2,
"children" => []
]
];
甚至可能更深,它是一个动态的树。
如下所示的预期结果,只需更改所有" name" " text"的关键。
$arr = [
0 => [
"id" => 1,
"text" => "departmentName",
"code" => "1",
"orderNumber" => 1,
"enabled" => true,
"createSpace" => false,
"description" => "balabala",
"lft" => 1,
"lvl" => 0,
"rgt" => 4,
"children" => [
0 => [
"id" => 7,
"text" => "departmentName_ProblemHere",
"code" => "7",
"orderNumber" => 7,
"enabled" => true,
"createSpace" => false,
"description" => null,
"lft" => 2,
"lvl" => 1,
"rgt" => 3,
"children" => []
]
]
],
1 => [
"id" => 2,
"text" => "departmentName",
"code" => "1",
"orderNumber" => 2,
"enabled" => true,
"createSpace" => false,
"description" => null,
"lft" => 1,
"lvl" => 0,
"rgt" => 2,
"children" => []
],
2 => [
"id" => 3,
"text" => "departmentName",
"code" => "1",
"orderNumber" => 3,
"enabled" => true,
"createSpace" => false,
"description" => null,
"lft" => 1,
"lvl" => 0,
"rgt" => 2,
"children" => []
],
3 => [
"id" => 4,
"text" => "departmentName",
"code" => "4",
"orderNumber" => 4,
"enabled" => true,
"createSpace" => false,
"description" => null,
"lft" => 1,
"lvl" => 0,
"rgt" => 2,
"children" => []
],
4 => [
"id" => 5,
"text" => "departmentName",
"code" => "5",
"orderNumber" => 1,
"enabled" => true,
"createSpace" => false,
"description" => null,
"lft" => 1,
"lvl" => 0,
"rgt" => 2,
"children" => []
],
5 => [
"id" => 6,
"text" => "departmentName",
"code" => "6",
"orderNumber" => 6,
"enabled" => true,
"createSpace" => false,
"description" => null,
"lft" => 1,
"lvl" => 0,
"rgt" => 2,
"children" => []
]
];