1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

multi dimensional array from func_get_args()

Discussion in 'PHP' started by freelanceDeveloper, Sep 25, 2015.

  1. #1
    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):

    SEMrush
     
    Solved! View solution.
    Last edited: Sep 25, 2015
    freelanceDeveloper, Sep 25, 2015 IP
    SEMrush
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,630
    Likes Received:
    724
    Best Answers:
    152
    Trophy Points:
    470
    #2
    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?
     
    PoPSiCLe, Sep 25, 2015 IP
  3. freelanceDeveloper

    freelanceDeveloper Member

    Messages:
    59
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    43
    #3
    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
     
    freelanceDeveloper, Sep 25, 2015 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,630
    Likes Received:
    724
    Best Answers:
    152
    Trophy Points:
    470
    #4
    $result[$array[0]][$array[1]];
    Code (markup):
    I'm not sure how you want that to be more beautiful or shorter...
     
    PoPSiCLe, Sep 25, 2015 IP
    freelanceDeveloper likes this.
  5. freelanceDeveloper

    freelanceDeveloper Member

    Messages:
    59
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    43
    #5
    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...;)
     
    Last edited: Sep 25, 2015
    freelanceDeveloper, Sep 25, 2015 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,030
    Likes Received:
    1,668
    Best Answers:
    235
    Trophy Points:
    515
    #6
    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.
     
    deathshadow, Oct 2, 2015 IP
    freelanceDeveloper likes this.
  7. freelanceDeveloper

    freelanceDeveloper Member

    Messages:
    59
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    43
    #7
    That i suppose would be the easiest solution indeed...
     
    freelanceDeveloper, Oct 3, 2015 IP
  8. freelanceDeveloper

    freelanceDeveloper Member

    Messages:
    59
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    43
    #8
    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
     
    freelanceDeveloper, Oct 3, 2015 IP
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,030
    Likes Received:
    1,668
    Best Answers:
    235
    Trophy Points:
    515
    #9
    deathshadow, Oct 3, 2015 IP
  10. #10
    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!
     
    Last edited: Oct 4, 2015
    ThePHPMaster, Oct 4, 2015 IP
    freelanceDeveloper likes this.
  11. freelanceDeveloper

    freelanceDeveloper Member

    Messages:
    59
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    43
    #11
    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):
     
    freelanceDeveloper, Oct 5, 2015 IP