1

I have a multidimensional array that's contains all user data , and I've build a function to get array value with the given key .

the problem is that the array is multidimensional array , and I don't know how many level .

this is the function

function getUserSessionData($key)
{

            $arrKeys = explode('.', $key);
            if(count($arrKeys) == 1){
                if(isset($_SESSION['user_data'][$arrKeys[0]])){
                    return $_SESSION['user_data'][$arrKeys[0]];
                }
            }
            else{
                if(isset($_SESSION['user_data'][$arrKeys[0]][$arrKeys[1]])){
                    return $_SESSION['user_data'][$arrKeys[0]][$arrKeys[1]];
                }
            }
            return 0;
}

and this is an example of the call.

getUserSessionData('profile.firstName');

The (.) indicates of level of the array . the function is support only tow levels .. is there any way to enhance this function so it can support more than tow levels ??

1
  • yeah, with a for loop after a count of $arrKeys Commented Nov 5, 2014 at 15:22

3 Answers 3

2

Sure, use a looping structure:

function getUserSessionData($key) {
    $parts = explode('.', $key);
    $data = $_SESSION["user_data"];
    while (count($parts) > 0) {
        $part = array_shift($parts);
        $data = $data[$part];
    }
    return $data;
}

Or independently of the session:

function resolveKey($array, $key) {
    $parts = explode('.', $key);
    while (count($parts) > 0) {
        $part = array_shift($parts);
        $array = $array[$part];
    }
    return $array;
}

echo resolveKey(array(
    "foo" => array(
        "bar" => array(
            "baz" => "ipsum"
        )
    )
), "foo.bar.baz"); // "ipsum"

echo resolveKey($_SESSION["user_data"], 'profile.firstName');
Sign up to request clarification or add additional context in comments.

Comments

0

Here's a PHP-Fiddle

function getUserSessionData($key){
    $arrKeys = explode('.', $key);
    $data = $_SESSION['user_data'];
    foreach($arrKeys as $k){
        if(isset($data[$k])) $data = $data[$k];
        else return false;
    }
    return $data;
}

Example usage:

session_start(); 
$_SESSION['user_data'] = []; 
$_SESSION['user_data']['user'] = []; 
$_SESSION['user_data']['user']['name'] = []; 
$_SESSION['user_data']['user']['name']['first'] = "robert"; 

echo getUserSessionData("user.name.first"); // echos "robert"

Comments

0

Thank you @Halcyon you've been very helpful.

but I've modified your function to get it to work . this is the new function

function getUserSessionData($key) {
$data = Yii::app()->session['account_data']['user_data'];
            $parts = explode('.', $key);
            while (count($parts) > 0) {
                $part = $parts[0];
                if(!isset($data[$part])){
                    return 0;
                }
                $data = $data[$part];
                array_shift($parts);
            }
            return $data;
}

1 Comment

I would recommend throwing an Exception when the path doesn't exist, rather than returning an arbitrary value.

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.