hi, This thing drives me crazy I want to build up an array based on the parameters from a functions function foo('MODULE','test' , 'the value'); Code (markup): $k = func_get_args(); // Array ( [0] => MODULE [1] => test ) $v = array_pop($k); // last parameter == the value $n = count($k); // 2 Code (markup): expected result $arr['MODULE']['test'] = $v; Code (markup):
What's the problem? You're not declaring the expected result anywhere, nor are you saying what you ARE getting as a result. Nor do you offer any insight to why this is needed. Sounds very convoluted and unnecessary to me - you already have all the contents of the array, why not just make an array?
I left out some code because it's irrelevant to the question (if the question is formulated well :-D ) ... It's part of a storage mechanism of module settings and stuff which kinda operates as the zend framework2... simply said : I'm looking for a beatifull way to transform $array = array([0] => MODULE [1] => test) Code (markup): into $result['MODULE']['test'] Code (markup): I tried different things but couldn't obtain the result in a "non ugly" way... I'm sure there must be a really simple way to achieve this but it's friday evening & I seem to Suffer from a severe blackout or maybe Altzeimer is at the doorstep :-D
$result[$array[0]][$array[1]]; Code (markup): I'm not sure how you want that to be more beautiful or shorter...
ok, that is a manual conversion... but as I mentioned in my first post, the first array is retrieved from $k = func_get_args(); so the nr of arguments (and by result nr of array items) is dynamic... so foo('arg1','arg2','val'); // last param always = value (so minimum 2 by all times) $result[$array[0]][$array[1]] = $array[2] Code (markup): foo('arg1','arg2','arg3','arg4', 'val'); $result[$array[0]][$array[1]][$array[2]][$array[3]] = $array[4] Code (markup): and that's where my brain freezes, although it looks simple You could do it your way and use switch based on the count but then again, you're limited to the nr of switches you add (although it never should go > 5 levels deep ) but I wanted to make this working witch no matter how many params but that I just can't figure out lol... Edit : when i say " function foo " it is in fact a setter method of a class not limited to 1 key / value pair but instead i would like the key to be a multidimentional key / value pair... i m gonna try the switch just to see how it works out but i ve found there are other and probably better ways to achieve this.. still curious how to dynamically convert the array so if anyone has a solution on that...
can you just pass the array instead of passing it as separate args? Rewrite the function you are passing it to to EXPECT an array. That or have it able to auto-detect if the value is an array with typeof.
But must i conclude there is no way to achieve such array conversion? You know, the're functions to change values into keys and stuff so i thought this would be possible but i abandoned trying as i couldn't get this right.. lol... noone is perfect :-p
Actually, I almost forgot -- all functions are also objects and have various properties and methods. Usually people use function.prototype.call, but there's another one -- function.prototype.apply https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply I believe that would do what you are asking. Pass it null so 'this' is empty, then the values you want. Should do the trick.
I pretty much gave up on this after spending an hour, my brains weren't working for some reason. Here are some solutions posted by some developers on stackoverflow: By Mark Baker: function foo() { $items = func_get_args(); $value = array_pop($items); $array = []; $arrayPtr = &$array; foreach ($items as $element) { $arrayPtr[$element] = null; $arrayPtr = &$arrayPtr[$element]; } $arrayPtr = $value; return $array; } var_dump(foo('arg1', 'argx', 'argz', 'value')); Code (markup): By VolkerK via recursion: function foo() { $items = func_get_args(); if ( 1==count($items) ) { return array_shift($items); } else { $key = array_shift($items); return array( $key=>call_user_func_array('foo', $items) ); } } var_dump(foo('arg1', 'argx', 'argz', 'value')); Code (markup): He also had another recursive solution using variadic function which are an alternative to func_get_args (works on PHP >= 5.6): function foo(...$items) { if ( 1==count($items) ) { return array_shift($items); } else { $key = array_shift($items); return array( $key=>call_user_func_array('foo', $items) ); } } var_dump(foo('arg1', 'argx', 'argz', 'value')); Code (markup): A solution posted by Federico: function foo() { $args = func_get_args(); $items = array_pop($args); foreach (array_reverse($args) as $item) { $items = array($item => $items); } return $items; } var_dump(foo('arg1', 'argx', 'argz', 'value')); Code (markup): Hopefully I understood your question correctly, either way it was a fun challenge!
Apparently I was very close to it ... but well .. not close enough But now it makes sense :-D Looks like you experienced the same "brain failure" i did :-D After an hour trying myself it kinda felt like I was even forgetting my own name, pretty scary ... Thanks!! ( +1 for you courage to put this on stackoverflow ) function foo(){ $items = func_get_args(); $value = array_pop($items); $array =[]; $arrayPtr =&$array; foreach($items as $element){ $arrayPtr[$element]=null; $arrayPtr =&$arrayPtr[$element];} $arrayPtr[$element]= $value; return $array;} var_dump(foo('arg1','argx','argz','value')); Code (markup):