Posted By

censmedia on 08/11/13


Tagged

parameters params


Versions (?)

Params creator and reader


 / Published in: PHP
 

URL: www.censmedia.nl

Simple function to create a params list type.

Example: $array = array('first'=>'The first one','second'=>'The second one');

$string = parse_params($array);

//output first=the first one;second=the second one

$string = 'first=the first one;second=the second one';

$array = read_params($string);

//output array('first'=>'The first one','second'=>'The second one')

  1. function read_params($params) {
  2. $params = explode(';',$params);
  3. foreach($params as $param) {
  4. $p = explode('=', $param);
  5. $pa[$p[0]] = $p[1];
  6. }
  7. $params = array_filter($pa);
  8. return $params;
  9. }
  10.  
  11. function parse_params($params,$gop=false) {
  12. if(is_array($params)){
  13. $d = array();
  14. foreach($params as $k=>$v){
  15. if(is_array($v)) $v = parse_params($v,true);
  16.  
  17. $d[] = ($gop ? '{'.$k.'='.$v.'}' : $k.'='.$v);
  18. }
  19. return implode( ($gop ? ',' : ';'),$d);
  20. }
  21. }

Report this snippet  

You need to login to post a comment.