【发布时间】:2018-07-13 03:02:33
【问题描述】:
如果我有这样的数组:
$data[0] = ['a' => 'b'];
$data[1] = ['c' => 'd'];
$data[2] = ['e' => 'f'];
如何在数组的特定键中添加更多数据,同时保留现有数据,例如
$data[0] = ['a' => 'b'];
$data[1] = ['c' => 'd', 'xx' => 'zz']; // New data has been added here.
$data[2] = ['e' => 'f'];
例如,我如何向$data[1] 添加内容?
我已阅读以下内容,但这些似乎不是答案:
- Push item to associative array in PHP
- How to push both value and key into array
- Insert new item in array on any position in PHP
我看过array_combine()、array_push() 和array_merge() 等方法,但似乎无法做到这一点。如果这是一个明显的问题,我深表歉意,但我已尝试查找上述内容但无法弄清楚。
【问题讨论】:
-
这个应该可以,不是吗? stackoverflow.com/questions/3797239/…
-
不,它没有。
array_splice($data, 1, 0, ['xx'=>'zz']);? -
您发布了指向您在Stack Overflow 上找到的一些意见的链接。但是你读过documentation吗? "Accessing array elements with square bracket syntax" 和 "Creating/modifying with square bracket syntax" 部分中描述了您的问题。
-
请发布您的一些代码,看看您是如何做到的,然后我们可以建议您哪里出错了..