1

I have a string 'value1/value2'. The required output is $_SESSION['value1']['value2']. i tried using explode and then array_reduce over explode values but with no success.

My code looks like

function set($key, $value){
/* code */
}

set('key1/key2', 'some_text');

required output like $_SESSION['key1']['key2'] = 'some_text';

key1/key2 is not fixed it may be 'key1' or 'key1/key2/key3' and so on. Anyone be make fiddle of it is Highly appreciable. Thanks

1
  • You shouldn't completely change the question, rendering answers to the original text useless...in this case from value access to assignment Commented Jul 6, 2017 at 13:04

3 Answers 3

2

Accessing a value via a key-path string, as in your original question, using your original idea and let array_reduce do the work looks like:

$session = ['value1' => [ 'value2' => [ 'value3' => 'there you are!' ]]];
$path = explode('/', 'value1/value2/value3');
$val = array_reduce($path, 
                    function(&$carry, $key) { return $carry[$key];},
                    $session);
echo $val
--> "there you are!"

Setting a value can be done e.g. like this, following the path by reference, creating arrays as needed:

function set($path, $value) {
    $path = explode('/', $path); 
    $key = array_pop($path); 
    $arr = &$_SESSION; 
    foreach($path as $part) {
        // carefull, this might lose values to accommodate
        // the structure wanted with $path
        (isset($arr[$part]) && is_array($arr[$part])) || ($arr[$part] = []); 
        $arr =& $arr[$part];
    } 
    $arr[$key] = $value;
};
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for help Tom. i have made some changes in line if(isset($arr[$part]) && is_array($arr[$part]) || ($arr[$part] = [])) $arr =& $arr[$part]; and this solves my problem for two array elements but it gives undesired results for three or more array elements. Can we code something like it gives desired results for any number of elements. for example key1/key2/key3 or key1/key2/key3/key4 and so on. For now its ok with 2 dimensional code.
This should work with paths of any size. I tested it with length 3. But there is a typo, you spotted the error, but the fix is wrong - I correct it to the intended form, let's see if this solves your problem.
If it doesn't (I added a missing )), I gladly have another look at why it seem to work for me, and might not (I tested it in psysh, with set as a closure)
Yes i got it. Now its works perfectly. Thanks for great idea.
1

Try this

<?php 
  session_start();   
  $string  = 'value1/value2';
  $array = explode("/",$string);
  $_SESSION[$array[0]][$array[1]] = "ccccccc";//$_SESSION['value1']['value2']

1 Comment

brother we can't add code statically like this $_SESSION[$array[0]][$array[1]] because 'value1/value2' is not fixed. there may be one or multiple / in string. Thanks for help.
1

For a general case (i.e. for more than two pieces), you'll need to iterate over the segments, and incrementally index further into your target array:

<?php
$string = 'value1/value2/value3';

$_SESSION = ['value1' => ['value2' => ['value3' => 'My String']]];

$target = $_SESSION;

foreach (explode('/', $string) as $piece) {
    $target =& $target[$piece];
}

echo $target; // My String

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.