Is there a PHP function for imploding an associative array without losing the keys?

梦想的初衷 提交于 2019-12-12 01:18:51

问题


The title of this question is self-explanatory.

I've heard I can mimic this using http_build_query, but I'd rather use a function that's meant for this.


Input example:

$assoc = array(
    "fruit" => "banana",
    "berry" => "blurberry",
    "vegetable" => "lettice"
);

Desired output (I get this with http_build_query):

string(46) "fruit=banana,berry=blurberry,vegetable=lettice"

output from reversal wanted is the same as input - that's my current problem.


回答1:


Implode with

serialize($array);

Explode with

unserialize($array);



回答2:


Found a function in the php .net comments for implode:

function implode_with_key($glue = null, $pieces, $hifen = ',') {
    $return = null;
    foreach ($pieces as $tk => $tv) $return .= $glue.$tk.$hifen.$tv;
    return substr($return,1);
}


来源:https://stackoverflow.com/questions/3821391/is-there-a-php-function-for-imploding-an-associative-array-without-losing-the-ke

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!