Home > Backend Development > PHP Tutorial > PHP怎么修改键名

PHP怎么修改键名

PHPz
Release: 2020-09-05 09:54:51
Original
2926 people have browsed it

PHP修改键名的方法:首先创建一个PHP文件;然后通过“foreach”语句循环遍历数组键名进行修改;最后通过“var_dump”打印出修改结果即可。

PHP怎么修改键名

PHP怎么修改键名?

Array
(
[city] => Array
(
[0] => 北京
[1] => 上海
[2] => 深圳
[3] => 广州
[4] => 武汉
[5] => 杭州
[6] => 郑州
[7] => 成都
[8] => 南京
[9] => 苏州
[10] => 西安
[11] => 长沙
[12] => 济南
[13] => 东莞
[14] => 厦门
[15] => 天津
)
)
Copy after login

问题:

如何修改键名,我要将数字12345789 变成 name?

方法:

首先数组是肯定要有key的,默认从0开始,除非你指定了key值。

提供两种解决方法:

1.

$inData = array(
    'city' => json_encode(array(
        '0' => '北京',
        '1' => '上海',
        '2' => '深圳',
        '3' => '广州',
        '4' => '武汉',
        '5' => '杭州',
        '6' => '郑州',
        '7' => '成都',
        '8' => '南京',
        '9' => '苏州',
        '10' => '西安',
        '11' => '长沙',
        '12' => '济南',
        '13' => '东莞',
        '14' => '厦门',
        '15' => '天津',
    )),
);
var_dump($inData);
Copy after login

2、

$inData = array(
        'city' => array(
            '0' => '北京',
            '1' => '上海',
            '2' => '深圳',
            '3' => '广州',
            '4' => '武汉',
            '5' => '杭州',
            '6' => '郑州',
            '7' => '成都',
            '8' => '南京',
            '9' => '苏州',
            '10' => '西安',
            '11' => '长沙',
            '12' => '济南',
            '13' => '东莞',
            '14' => '厦门',
            '15' => '天津',
        ),
    );
    foreach ($inData['city'] as $v) {
        $inData['city'][$v] = $v;
    }
    var_dump($inData);
Copy after login

可以有很多种方法,array_combine,array_flip后再array_flip。

更多相关技术知识,请访问PHP中文网

Related labels:
php
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