note 67273 deleted from ref.array by levim

From: Date: Tue, 06 May 2014 22:01:11 +0000
Subject: note 67273 deleted from ref.array by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-199442@lists.php.net to get a copy of this message
Note Submitter: vinaur at gmail dot com ---- Regarding the array to string (parse_line) and string to array (parse_array) functions posted below by Kevin Law. The functions will not work correctly if the array being parsed contains values that include commas and possibly parentheses. To solve this problem I added urlencode and urldecode functions and the result looks like this: <?php function parse_line($array){ $line = ""; foreach($array AS $key => $value){ if(is_array($value)){ $value = "(". parse_line($value) . ")"; } else { $value = urlencode($value); } $line = $line . "," . urlencode($key) . ":" . $value . ""; } $line = substr($line, 1); return $line; } function parse_array($line){ $q_pos = strpos($line, ":"); $name = urldecode(substr($line,0,$q_pos)); $line = trim(substr($line,$q_pos+1)); $open_backet_pos = strpos($line, "("); if($open_backet_pos===false || $open_backet_pos>0){ $comma_pos = strpos($line, ","); if($comma_pos===false){ $result[$name] = urldecode($line); $line = ""; }else{ $result[$name] = urldecode(substr($line,0,$comma_pos)); $result = array_merge($result, parse_array(substr($line,$comma_pos+1))); $line = ""; } }else if ($open_backet_pos==0){ $line = substr($line,1); $num_backet = 1; $line_char_array = str_split($line); for($index = 0; count($line_char_array); $index++){ if($line_char_array[$index] == '('){ $num_backet++; }else if ($line_char_array[$index] == ')'){ $num_backet--; } if($num_backet == 0){ break; } } $sub_line = substr($line,0,$index); $result[$name] = parse_array($sub_line); $line = substr($line,$index+2); } if(strlen($line)!=0){ $result = array_merge($result, parse_array($line)); } return $result; } ?>

« previous php.notes (#199442) next »