如何在PHP数组的键和值相结合 [英] How to combine the keys and values of an array in PHP

查看:211
本文介绍了如何在PHP数组的键和值相结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有键/值对PHP数组:

 阵列('富'=>'酒吧','巴兹'=>'qux');

什么是改造这个一个数组,看起来像下面简单的办法?

 阵列('富=酒吧,巴兹= qux');

 阵列(0 =>'富=酒吧,1 =>'巴兹= qux');

在Perl中,我会做这样的事情。

 地图{$ _ = $ {哈希$ _}键%哈希

有没有这样的事情在阵列功能一整套的中PHP?没有我看着似乎是一个方便的解决方案。


解决方案

 函数parameterize_array($数组){
    $ OUT =阵列();
    的foreach($数组$关键=> $值)
        $出[] =$键= $价值;
    返回$出;
}

Say I have an array of key/value pairs in PHP:

array( 'foo' => 'bar', 'baz' => 'qux' );

What's the simplest way to transform this to an array that looks like the following?

array( 'foo=bar', 'baz=qux' );

i.e.

array( 0 => 'foo=bar', 1 => 'baz=qux');

In perl, I'd do something like

map { "$_=$hash{$_}" } keys %hash

Is there something like this in the panoply of array functions in PHP? Nothing I looked at seemed like a convenient solution.

解决方案

function parameterize_array($array) {
    $out = array();
    foreach($array as $key => $value)
        $out[] = "$key=$value";
    return $out;
}

这篇关于如何在PHP数组的键和值相结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆