本文介绍了转换包含字符串键和值到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
没有人知道把这个字符串的elagant方式(名单并不明确。该钥匙和价值观,可以任意添加)
business_type,咖啡厅| business_type_plural,咖啡馆| sample_tag,沙发| BUSINESS_NAME,网吧沙发
在此数组?
阵列(
[business_type] =>'咖啡厅'
[business_type_plural] => 网吧
[sample_tag] =>'沙发'
[BUSINESS_NAME] =>'沙发上的咖啡馆
)
解决方案
爆炸了!
$ finalArray =阵列();$ asArr =爆炸('|',$字符串);的foreach($ asArr为$ VAL){
$ TMP =爆炸('',$ VAL);
$ finalArray [$ TMP [0] = $ TMP [1];
}的print_r($ finalArray);
does anyone know an elagant way of turning this string (the list is not definite.. the "keys" and "values" can be added arbitrarily)
business_type,cafe|business_type_plural,cafes|sample_tag,couch|business_name,couch cafe
into this array?
array(
[business_type]=>'cafe'
[business_type_plural] => 'cafes'
[sample_tag]=>'couch'
[business_name]=>'couch cafe'
)
解决方案
Explode it!
$finalArray = array();
$asArr = explode( '|', $string );
foreach( $asArr as $val ){
$tmp = explode( ',', $val );
$finalArray[ $tmp[0] ] = $tmp[1];
}
print_r( $finalArray );
这篇关于转换包含字符串键和值到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!