1

I am trying to send specific indexes from arrays by string to some php code to describe the data that I need to combine. The problem I am having is that SESSION does not seem to be available within the function Str_To_Array. What am I missing about scope here? Also if anybody can recommend a better way I would be every so grateful. PS i added the include in case including the function causes any abnormalities.

function Str_To_Array($str) {

    $d = explode(':',$str) ;

    print_r($d[0]) ;

    $t = $d[0] ;

    $n = $$t ;

    if( !isset( $d[1] ) ) { return $n ; }

    $n = $n[$d[1]] ;

    return $n ;
}

include(DIR_ROOT . "php_function/Str_To_Array.php") ;

$test = '_SESSION' ;
$ARRANGE = Str_To_Array($test) ;<----this says _SESSION is undefined
print_r($ARRANGE) ;
$ARRANGE = $$test ;<----this works
print_r($ARRANGE) ;
7
  • 2
    Have you added session_start() on top of your script? Commented Dec 5, 2012 at 1:19
  • Yea. I was wondering if that had something to do with it as well, thats why I asked the question if maybe it is outside the scope. It is defined when I just use $ARRANGE = $$test. Commented Dec 5, 2012 at 1:23
  • Don't use variable variables - they bring troubles. Commented Dec 5, 2012 at 1:32
  • Not sure why youre doing that Str_To_Array function, but you can add global $$t; after $t = $d[0]; line. Commented Dec 5, 2012 at 1:34
  • i need to order data in session by dates and add markers for missing time periods. The data is stored across different session indexes. The specific pages use different indexes but differently share each others data. I wanted to just send a string with the post data that told the server what indexes to use to combine. Commented Dec 5, 2012 at 1:36

2 Answers 2

1

It loos like it's the problem of PHP, just tried the code below, when in a function, ${'_SESSION'} works and $$t don't work. This only happens to $_SESSION but not the other super global $_POST and $_GET etc.

<?php
session_start();

function foo() {
  $t = '_SESSION';
  $a = $$t;             // not work
  $b = ${'_SESSION'};   // works
  var_dump($a, $b);
}

foo();
Sign up to request clarification or add additional context in comments.

1 Comment

Wow thats interesting. I normally assume its something Im doing wrong. This is the issue though. I guess I have no way to accomplish this then. Thanks for clearing it up.
0

Hard to follow the purpose here, however this should work

function getVar($str)
{
    $vars = get_defined_vars();
    $d = explode(':',$str) ;
    if (isset($vars[$d[0]]) || array_key_exists($vars[$d[0]])) {
        if (!empty($d[1]) && (isset($vars[$d[0]][$d[1]]) || array_key_exists($vars[$d[0]][$d[1]]))
            return $vars[$d[0]][$d[1]];
        return $vars[$d[0]];
    }
    return null;
}

1 Comment

Interesting work around. I tried finding _SESSION registered in Globals but it wouldnt transfer either. This makes it work though. Cheers.

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.