如何在PHP中将以下参数分解为数组?

时间:2012-11-08 22:53:30

标签: php arrays

我有以下参数:

max_image_width=100,max_image_height=200,image_proportion=1.75

我希望得到一个数组:

array('max_image_width'=>100,'max_image_height'=>200,'image_proportion'=175);

4 个答案:

答案 0 :(得分:6)

$str = 'max_image_width=100,max_image_height=200,image_proportion=1.75';
$cfg = parse_ini_string(
    str_replace(',', "\n", $str)
);

print_r($cfg);

答案 1 :(得分:3)

5.4

$a=[];foreach(explode(',',$i)as$b){$a[explode('=',$b)[0]]=explode('=',$b)[1];}

答案 2 :(得分:3)

$output = array();
parse_str(str_replace(',', '&', 'max_image_width=100,max_image_height=200,image_proportion=1.75'), $output);

答案 3 :(得分:2)

E.g。使用preg_match_all

<?php
$t = 'max_image_width=100,max_image_height=200,image_proportion=1.75';

preg_match_all('!([^=]+)=([^,]+)!', $t, $m);
$x = array_combine($m[1], $m[2]);

var_export($x);

打印

array (
  'max_image_width' => '100',
  ',max_image_height' => '200',
  ',image_proportion' => '1.75',
)

(虽然没有正则表达式,还有很多其他方法可以做到这一点;-))