Hallo,
vor kurzem für einen Kollegen geschrieben, jetzt sogar selbst mal gebraucht:
Ein Skript, das einen String an einem vordefinierten Zeichen aufbricht und so einen mehrdimensionalen Array erzeugt (createArrayByString, string2array sozusagen) oder versucht, darauf zuzugreifen (accessArrayByString).
Praktisches Beispiel:
config.ini Code:
[path]
classes = library/classes
functions = library/functions
parse_ini_file() mit $process_sections = TRUE erzeugt daraus einen Array
array("path" => array("classes" => "library/classes", "functions" => "library/functions"))
Auf diesen möchte ich in XPath-Manier mit $config->get("path/functions") zugreifen.
Hier die beiden Funktionen:
createArrayByString.php PHP-Code:
<?php
/**
* creates an array by splitting a string for key usage on a given separator
*
* @example
* <code>
* $string = "one.two.three.four";
* $separator = ".";
*
* $array = createArrayByString($string, $separator, "standard value");
*
* var_dump($array); // should return array("one" => array("two" => array("three" => array("four" => "standard value"))))
* </code>
*
* @param string $string string to use for keys
* @param char $separator char used for exploding the string
* @param mixed $value the string is used for the keys in the new array, this is used for the value
* @return array
*/
function createArrayByString($string, $separator, $value = NULL)
{
if (strlen($string) > 0) {
$splitter = explode($separator, $string);
$index = array_shift($splitter);
$function = __FUNCTION__;
return array($index => $function(implode($separator, $splitter), $separator, $value));
}
return $value;
}
?>
accessArrayByString.php PHP-Code:
<?php
/**
* access an array by splitting the given string, using the splitters as array keys
*
* @example
* <code>
* $array = array("one" => array("two" => array("three" => array("four" => "standard value"))))
* $string = "one.two.three.four";
* $separator = ".";
* $value = accessArrayByString($array, $string, $separator, NULL);
* var_dump($value); // should return "standard value";
* </code>
*
* @param array $array
* @param string $string the string splitters will be used as array keys
* @param char $separator char used to split the array
* @param mixed $default value that will be returned in case the array has not such keys (as given in the array)
* @return mixed value, accessed by the keys
*/
function accessArrayByString($array, $string, $separator, $default = NULL)
{
if (!is_array($array)) {
return empty($string) ? $array : $default;
}
@list ($key, $rest) = explode($separator, $string, 2);
$function = __FUNCTION__;
return array_key_exists($key, $array)
? $function($array[$key], $rest, $separator, $default)
: $default;
}
?>
Wie man sieht arbeiten beide rekursiv, die Funktionsnamen können aber ohne weitere Änderungen angepasst werden.
Hier nochmal das zusammengefasste, anschauliche Beispiel:
PHP-Code:
<?php
$string = "one.two.three.four";
$separator = ".";
$array = createArrayByString($string, $separator, "standard value");
$return = accessArrayByString($array, $string, $separator, "element not found");
var_dump($array); // should return array("one" => array("two" => array("three" => array("four" => "standard value"))))
var_dump($return); // should return "standard value";
?>