function test_array_to_string() { $arr = array('A', 'B', 'indC' => 'C', array('D', 'E'), 'indF' => 'F'); $parse = sonots::array_to_string($arr); $truth = 'A,B,indC:C,(D,E),indF:F'; $this->assertEqual($parse, $truth); $arr = (array) 'A'; $parse = sonots::array_to_string($arr); $truth = 'A'; $this->assertEqual($parse, $truth); $arr = array(); $parse = sonots::array_to_string($arr); $truth = ''; $this->assertEqual($parse, $truth); }
/** * Convert an array to a string. * * PHP API Extension * * Example * <code> * $arr = array('A', 'B', 'indC' => 'C', array('D', 'E'), 'indF'=>'F'); * echo array_to_string($arr); * </code> * Output: * <code> * A,B,indC:C,(D,E),indF:F * </code> * * @access public * @static * @param array $array * @param string $hashsep A character to be used as a hash key and val seperator * @param string $elemsep A character to be used as a elememt separator * @param string $openarray A character to be used as an open bracket of an array * @param string $closearray A character to be used as a close bracket of an array * @param boolean $encode Performe encode for key/val or not * Note: encoding is usually necessary especially when you want * to use delimiter characters in keys and values * @return string * @see string_to_array * @version $Id: v 1.3 2008-07-15 11:14:46 sonots $ */ function array_to_string($array, $hashsep = ':', $elemsep = ',', $openarray = '(', $closearray = ')', $encode = true) { $string = ""; $delims = $hashsep . $elemsep . $openarray . $closearray; foreach ($array as $key => $value) { if (is_array($value)) { $value = sonots::array_to_string($value, $hashsep, $elemsep, $openarray, $closearray, $encode); $value = $openarray . $value . $closearray; } else { $value = $encode ? sonots::urlencode($value, $delims) : $value; } if (is_int($key)) { $string .= $elemsep . $value; } else { $key = $encode ? sonots::urlencode($key, $delims) : $key; $string .= $elemsep . $key . $hashsep . $value; } } $string = substr($string, 1); return $string; }
/** * Recover option line, but into GET argument style such as * opt1=val1&opt2=val2&opt3=(a&b) * Note that this is not inverse of parse_uri_option_line exactly * because parse_uri_option_line assumes input variables are already * rawurldecoded, but this performs rawurlencode. * * @access public * @static * @param array $options * @return string * @see glue_option_line * @see parse_uri_option_line * @version $Id: v 1.0 2008-07-16 11:14:46 sonots $ * @since v 1.8 */ function glue_uri_option_line($options) { $array = PluginSonotsOption::boolean_to_numeric($options); $string = sonots::array_to_string($array, '=', '&', '(', ')', true); $args = explode('&', $string); foreach ($args as $i => $arg) { $vars = explode('=', $arg); $vars = array_map('rawurlencode', $vars); $args[$i] = implode('=', $vars); } return implode('&', $args); }