diff --git a/README.md b/README.md index fd89609..82b97ff 100644 --- a/README.md +++ b/README.md @@ -407,7 +407,7 @@ Merge the contents of two or more objects or arrays together into the first one. ##### `\DDTools\ObjectTools::unfold($params)` -Converts a multidimensional array/object into an one-dimensional one joining the keys with `'.'`. +Converts a multidimensional array/object into an one-dimensional one joining the keys with `$params->keySeparator`. For example, it can be helpful while using placeholders like `[+size.width+]`. * `$params` @@ -424,6 +424,11 @@ For example, it can be helpful while using placeholders like `[+size.width+]`. * `arrayAssociative` * **Required** +* `$params->keySeparator` + * Desctription: Separator between nested keys in the result object/array. + * Valid values: `string` + * Default value: `'.'` + * `$params->keyPrefix` * Desctription: Prefix of the keys of an object/array (it's an internal varible, but can be used if required). * Valid values: `string` @@ -1072,6 +1077,31 @@ array ( ``` +##### Use custom key separator + +```php +var_export(\DDTools\ObjectTools::unfold([ + 'object' => [ + 'name' => 'Elon Musk', + 'parents' => [ + 'mother' => 'Maye Musk', + 'father' => 'Errol Musk' + ] + ], + 'keySeparator' => '_' +])); +``` + +Returns: + +```php +stdClass::__set_state(array ( + 'name' => 'Elon Musk', + 'parents_mother' => 'Maye Musk', + 'parents_father' => 'Errol Musk' +)) + + #### `\DDTools\ObjectTools::isPropExists($params)` Checks if the object, class or array has a property / element using the same syntax. diff --git a/src/ObjectTools/ObjectTools.php b/src/ObjectTools/ObjectTools.php index dc8f730..38ec36c 100644 --- a/src/ObjectTools/ObjectTools.php +++ b/src/ObjectTools/ObjectTools.php @@ -364,7 +364,7 @@ public static function extend($params){ /** * unfold - * @version 1.0 (2021-11-16) + * @version 1.1 (2021-11-17) * * @see README.md * @@ -375,6 +375,7 @@ public static function unfold($params){ 'objects' => [ //Defaults (object) [ + 'keySeparator' => '.', 'keyPrefix' => '', //The internal parameter, should not be used outside. Used only in child calls of recursion. 'isSourceObject' => null @@ -419,7 +420,7 @@ public static function unfold($params){ 'keyPrefix' => $params->keyPrefix . $key . - '.' + $params->keySeparator , 'isSourceObject' => $isSourceObject ])