一个二维数组如下 $list
Array
(
[0] => Array
(
[article_id] => 272
[attachment] => 1222.txt
[article_name] => 测试分类类目
[erp_account] => AG10083
[email] => test1004@sailvan.com
[time] => 1445912135
)
[1] => Array
(
[article_id] => 273
[attachment] => api.action.php
[article_name] => Hyd测试1
[erp_account] => AG44464
[email] => testhyd@qq.com
[time] => 1445493285
)
[2] => Array
(
[article_id] => 273
[attachment] => apiOpen.action.php
[article_name] => Hyd测试1
[erp_account] => AG44464
[email] => testhyd@qq.com
[time] => 1445485976
)
)
现在打算在数组前添加一个序号:
Array
(
[0] => Array
(
[nums] => 0
[article_id] => 272
[attachment] => 1222.txt
[article_name] => 测试分类类目
[erp_account] => AG10083
[email] => test1004@sailvan.com
[time] => 1445912135
)
代码如下:
//使用引用代替$list[$k]
foreach ($list as $k=>&$v){
array_unshift($v['nums'],$k);
}
发现增加不了!!!..原因是array_unshift不能创建键(nums),然后增加一个值进去
如果直接array_unshift($v,$k);就可以,但是不是要的效果
改进如下,使用数组合并的方法代替
foreach ($list as $k => &$v){
$v = array_merge(array('nums' => $k),$v);
}
结果如下:
Array
(
[0] => Array
(
[nums] => 0
[article_id] => 272
....
)
[1] => Array
(
[nums] => 1
[article_id] => 273
....
)
[2] => Array
(
[nums] => 2
[article_id] => 273
....
)
)