How to modify the array key name in php: first create a php sample code file; then create the array; then define a foo method; and finally use "array_combine" and other functions to modify the array key name.
php modify the array key name
$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);
The running result is:
Array ( [0] => Array ( [StaffId] => 50 [Wage] => 60 [Name] => long [Work] => zzz [Type] => kkk ) [1] => Array ( [StaffId] => 60 [Wage] => 70 [Name] => king [Work] => lll [Type] => ttt ) [2] => Array ( [StaffId] => 70 [Wage] => 80 [Name] => quit [Work] => qqq [Type] => xxx ) )
Related introduction:
array_combine() function creates a new array by merging two arrays, where the elements of one array are key names and the elements of the other array are key values.
Note: The number of elements in the key name array and the key value array must be the same!
Syntax
array_combine(keys,values);
Parameters
keys required. Specifies the key name of the array.
values Required. Specifies the key value of the array.
Recommended: "PHP Tutorial"
The above is the detailed content of How to modify the php array key name. For more information, please follow other related articles on the PHP Chinese website!