How to modify array key name in php

墨辰丷
Release: 2023-03-27 17:58:01
Original
2312 people have browsed it

This article mainly introduces the method of modifying the key name of the array in PHP, involving PHP's operation skills for array traversal, interception, conversion and other related operations. Friends in need can refer to the following

The example of this article describes the modification of PHP Method for array key names. Share it with everyone for your reference, the details are as follows:

$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);
Copy after login

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
    )
)
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHPProgrammer’s Technology Growth Plan_php Digest

php uses regular expressions to determine whether it is a number_php skills

##PHPJudges the FORM form or URL parameters Method to determine whether the data is an integer_php tips

The above is the detailed content of How to modify array key name in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template