DeyaZ
DeyaZ

Reputation: 63

Multidimensional Array search by string

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 ??

Upvotes: 1

Views: 59

Answers (3)

DeyaZ
DeyaZ

Reputation: 63

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;
}

Upvotes: 0

Halcyon
Halcyon

Reputation: 57709

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');

Upvotes: 2

I wrestled a bear once.
I wrestled a bear once.

Reputation: 23379

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"

Upvotes: 0

Related Questions