Home > php教程 > php手册 > body text

快速将PHP数组键值组合成字符串

WBOY
Release: 2016-06-06 20:08:42
Original
2887 people have browsed it

最近在写MVC碰到一个问题:可以使用implode快速将PHP数组组合成字符串。但是如果想要将键值快速输出成字符串呢? 临时想到的方法是循环 假设有一个一维数组$a 存放N个内容. 代码一: ?php$b = '';foreach($a as $key=$value){$b.=$key;}echo $b;? 代码一的写

最近在写MVC碰到一个问题:可以使用implode快速将PHP数组组合成字符串。但是如果想要将键值快速输出成字符串呢?

临时想到的方法是循环

假设有一个一维数组$a 存放N个内容.

代码一:

<?php $b = '';
foreach($a as $key=>$value){
$b.=$key;
}
echo $b;
?>
Copy after login

代码一的写法太痛苦了,而且PHP本身有一个函数implode可以将数组快速组成字符串,那么键值也必定可以的。重新看了一下数组函数的手册,找到array_keys可以直接返还数组中所有键值。手册地址:http://docs.php.net/manual/en/function.array-keys.php

代码二:

<?php echo implode("", array_keys($a));
?>
Copy after login

哈哈,代码是不是简洁多了?:)

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template