Home > Article > Backend Development > How to convert the key (key name) in the array to a string in php
Conversion method: 1. Use the array_keys() function to obtain all keys in the array, and return the obtained array key names in the form of an array; 2. Use the implode() function to convert the key array to String, syntax "implode(array_keys($arr))".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php Convert the keys (key names) in the array to strings
In PHP, if you want to convert all the key names in the array to strings, you need two steps:
First use the array_keys() function to obtain all key names in the array, and return the obtained array key names in array form.
Then use the implode() function to convert the key array into a string
Implementation code:
<?php header("Content-type:text/html;charset=utf-8"); $arr=array("Name"=>"Peter","Age"=>"41","Country"=>"USA"); var_dump($arr); $keys=array_keys($arr); var_dump($keys); $str=implode($keys); var_dump($str); ?>
Recommended learning: "PHP Video Tutorial", "PHP ARRAY"
The above is the detailed content of How to convert the key (key name) in the array to a string in php. For more information, please follow other related articles on the PHP Chinese website!