#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    37
    Rep Power
    2

    Assigning values to associative array with dynamic keys


    Hi there,

    I have $options as an associative array with each value as mixed(can be strings, or other arrays). I won't have any objects there.
    $keys is a numeric array & the number of keys is determined at runtime.

    I want to have a result similar to this expression
    $options[$keys[0]][$keys[1]].......[$keys[count($keys)-1]] = $value;

    For example, if $keys = array('key1'), i want to do
    $options['key1'] = $value;
    & if $keys = array('key1', 'key2'), i want to do
    $options['key1']['key2'] = $value; & so on

    The problem with array_replace_recursive is that $value may itself be an array. Someway to constrain the depth to which array_replace_recursive can go? or maybe some other way?
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    PHP Code:
    $keys = array('a''b');
    $val 123;

    $curr $val;
    foreach (
    array_reverse($keys) as $key)
        
    $curr = array($key => $curr);
        
    var_dump$curr ); 
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    37
    Rep Power
    2
    Thanks a lot

IMN logo majestic logo threadwatch logo seochat tools logo