Home  >  Article  >  Backend Development  >  How to convert the key (key name) in the array to a string in php

How to convert the key (key name) in the array to a string in php

青灯夜游
青灯夜游Original
2022-03-02 19:15:273412browse

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))".

How to convert the key (key name) in the array to a string in php

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);
?>

How to convert the key (key name) in the array to a string in php

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!

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