6

is there an easy, non-eval-using method for getting a reference to an element of a multidimensional array? The key should be passed as a string.

Here's an example:

getSessionReference('1.4.2', $arrReference);

should return a reference to

$_SESSION['1']['4']['2']

and so a call like

$arrReference['foo'] = 'bar';

would change it to

$_SESSION['1']['4']['2']['foo'] = 'bar'

Any ideas?

Thanks in advance.

6
  • 1
    +1 for clarity. But I have to ask if you can you give a real example of what you're trying to achieve? Commented Jun 7, 2014 at 20:32
  • @bestprogrammerintheworld Whatever he tries to do, he does it completely wrong Commented Jun 7, 2014 at 20:40
  • @Scorpion - maybe so, but in that case we're here to help him! :-) Commented Jun 7, 2014 at 20:41
  • 1
    @bestprogrammerintheworld He would not listen to us :) Commented Jun 7, 2014 at 20:42
  • A good real world example is wanting to give the code a reference to only a part of the session array. This way, you keep storage of session vars for different code bases and modules well separated. In large projects, especially ones which evolve over years with many developers coming and going, these types of engineering decisions can sometimes be useful. Commented Jun 7, 2014 at 20:48

3 Answers 3

7
$arr[5][6][7] = 111;

$cursor =& $arr;
foreach (explode('.', '5.6') as $key) {
    $cursor =& $cursor[$key];
}

var_dump($arr);
var_dump($cursor);
$cursor['foo'] = 5;
var_dump($arr);
var_dump($cursor);

http://codepad.viper-7.com/XUEhMj

or in function form

function & getSessionRef($keyPath) {
    $cursor =& $_SESSION;
    foreach (explode('.', $keyPath) as $key) {
        $cursor =& $cursor[$key];
    }
    return $cursor;
}

$cursor =& getSessionRef('a.6');

btw - I used the php feature named references in that code, where you see the ampersand like =&.

Sign up to request clarification or add additional context in comments.

3 Comments

hey, shouldn't 'foo' be an index in the "7" array?
@Charles why? I didn't specify any index 7 in my key path for the reference.
@rambocoder That's 99 percent the approach I used before, but I messed up the referencing somehow, so each loop cycle added trash to the session. :-/ Thanks!
1

Use pass-by-reference.

function getReference($key, &$arr)
{
    $e = explode('.', $key);
    foreach ($_SESSION[$e[0]][$e[1]][$e[2]] as $k => &$v)
        $arr[$k] = $v;
}

$arr = array();
getReference("1.4.2", $arr);

p.s.: this does not actually return the reference but it serves your needs.

Comments

0

This should do what you are after:

function &getSessionReference($keyStr, &$session) {
    $keys = explode('.', $keyStr);
    $temp = &$session;
    foreach ($keys as $key) {
        $temp = &$temp[$key];
    }
    return $temp;
}

$_SESSION['1']['4']['2'] = [];
$arrReference = &getSessionReference('1.4.2', $_SESSION);
$arrReference['foo'] = 'bar';

echo json_encode($_SESSION);

// {"1":{"4":{"2":{"foo":"bar"}}}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.