Home >Backend Development >PHP Tutorial >怎么将更改PHP二维数组的键名

怎么将更改PHP二维数组的键名

WBOY
WBOYOriginal
2016-06-13 10:26:111344browse

如何将更改PHP二维数组的键名
我在代码里的得到一个数组如下:
1 2 3 4 5 6 7(键名)
-----------------------------------
a 50 60 long zzz kkk ooo
b 60 70 king lll ttt ppp
c 70 80 quit qqq xxx ccc
------------------------------------
我想取出2,3,4,5,6这五列建立新的数组,并且新的数组用字符串作为键名
如下:
StaffId Wage Name Work Type
-----------------------------
50 60 long zzz kkk
60 70 king lll ttt
70 80 quit qqq xxx
-----------------------------
请问有什么好的方法?初学PHP,对二维数组并不是很懂,请教各位了。

------解决方案--------------------
将数组 var_export 出来。 我看不懂你的数组是什么结构
------解决方案--------------------
array_slice($arr,1,-1);截取出来再改键名
------解决方案--------------------
完整的写法

PHP code
$ar = array(  array(1 => 'a', 2 => 50, 3 => 60, 4 => 'long', 5 => 'zzz', 6 => 'kkk', 7 => 'ooo'),  array(1 => 'b', 2 => 60, 3 => 70, 4 => 'king', 5 => 'lll', 6 => 'ttt', 7 => 'ppp'),  array(1 => 'c', 2 => 70, 3 => 80, 4 => 'quit', 5 => 'qqq', 6 => 'xxx', 7 => 'ccc'),);$kname = array('StaffId', 'Wage', 'Name', 'Work', 'Type');function foo(&$v, $k, $kname) {  $v = array_combine($kname, array_slice($v, 1, -1));}array_walk($ar, 'foo', $kname);print_r($ar);<br><font color="#e78608">------解决方案--------------------</font><br>
探讨

完整的写法PHP code
$ar = array(
array(1 => 'a', 2 => 50, 3 => 60, 4 => 'long', 5 => 'zzz', 6 => 'kkk', 7 => 'ooo'),
array(1 => 'b', 2 => 60, 3 => 70, 4 => 'king', 5 => 'lll', 6 => 'ttt', 7 => 'ppp'),……
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn