You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
and, ideal, is to add it as a internal method, to call in standard way $m->objs2list(). So, this is the suggestion.
Suggested helper
/** * Transforns objects into array of "_key" and "_val" objects. * The object can be pointed by a $path (array of reference-labels). */functionobjs2list($obj,$path=NULL) {
$l = [];
$flag = ($path && count($path));
foreach(($flag? arrayPath($obj, $path): $obj) as$k=>$v)
$l[] = ['_key'=>$k,'_val'=>$v];
if ($flag) {
arrayPath($obj, $path, $l); return$obj;
} elsereturn$l;
}
/** * set/return a nested array value * * @param array $array the array to modify * @param array $path the path to the value * @param mixed $value (optional) value to set * * @return mixed previous value * @see https://stackoverflow.com/a/36042293/287948 */functionarrayPath(&$array, $path = array(), &$value = null)
{
$args = func_get_args();
$ref = &$array;
foreach ($pathas$key) {
if (!is_array($ref)) {
$ref = array();
}
$ref = &$ref[$key];
}
$prev = $ref;
if (array_key_exists(2, $args)) {
// value param was passed -> we're setting$ref = $value; // set the value
}
return$prev;
}
The text was updated successfully, but these errors were encountered:
ppKrauss
changed the title
Add a helper to convert dictionary into array of dictionary of key-value
Add a helper for anonymous reference of object's key-values
Jun 14, 2018
Solving the problem of "no anonymous reference for objects"... Example:
works fine with anonymous reference for array. But when using a "dictionary" like associative array,
it is impossible with Mustache. A workaround is to use a function that transforms the
$abc
dictionary into keys and vals,Another problem is to transform not the full root-dictionary, but only the object pointed by a path, example:
and, ideal, is to add it as a internal method, to call in standard way
$m->objs2list()
. So, this is the suggestion.Suggested helper
The text was updated successfully, but these errors were encountered: