平台严格禁止发布违法/不实/欺诈等垃圾信息,一经发现将永久封禁帐号,针对违法信息将保留相关证据配合公安机关调查!
// 假设有一个二维数组
$twoDimensionalArray = [
['name' => 'Alice', 'age' => 25],
['name' => 'Bob', 'age' => 30],
['name' => 'Charlie', 'age' => 23]
];
// 要插入的新键值对
$newKeyValuePair = ['gender' => 'female'];
// 使用array_map和一个匿名函数来插入新的键值对
$updatedArray = array_map(function($item) use ($newKeyValuePair) {
return array_merge($item, $newKeyValuePair);
}, $twoDimensionalArray);
print_r($updatedArray);
这段代码会在原有的二维数组的每个子数组中插入新的键值对,输出结果将是:
Array
(
[0] => Array
(
[name] => Alice
[age] => 25
[gender] => female
)
[1] => Array
(
[name] => Bob
[age] => 30
[gender] => female
)
[2] => Array
(
[name] => Charlie
[age] => 23
[gender] => female
)
)