本文介绍了将包含键和值的数组转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含键和值的数组。我想转换为字符串。

I have an array it contains key and value. i would like to converting into a string.

array(
 [business_type]=>'cafe'
 [business_type_plural] => 'cafes'
 [sample_tag]=>'couch'
 [business_name]=>'couch cafe'
 )

预期输出:

business_type,cafe|business_type_plural,cafes|sample_tag,couch|business_name,couch cafe

注意:

我在StackOverflow中搜索时,发现以下问题,并且有答案。我要完全相反。

I was searching in StackOverflow and found the below question and it has answer. I want exactly reverse one.

推荐答案

尝试

$data = array();
foreach($arr as $key=>$value) {
  $data[] = $key.','.$value;
}
echo implode('|',$data);

另一个解决方案:

function test_alter(&$item1, $key, $delimiter)
{
    $item1 = "$key".$delimiter."$item1";
}

array_walk($arr, 'test_alter',',');
echo implode('|',$arr);

这篇关于将包含键和值的数组转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 14:16