2

What happen if it is a very big array? One would try to save memory. The next example makes copies of parts of the array, but it should be not necessary, since the array is a global variable.

function arrayTraverse($key) {
    global $someArray;
    $keys = func_get_args();
    $arrayPart = $someArray;

    foreach ($keys as $key) {
        $arrayPart = $arrayCopy[$key];
    }

    $value = $arrayPart;
    return $value;
}

Usage example:

$someArray = [];
$someArray['aKey'] = [];
$someArray['aKey']['someOtherKey'] = [];
$someArray['aKey']['someOtherKey'][5] = [];
$someArray['aKey']['someOtherKey'][5]['value'] = 'hello';
echo arrayTraverse('aKey', 'someOtherKey', 5, 'value'); // hello
mikl
  • 1,067
  • 1
  • 20
  • 34
  • so basically provide a bunch of keys to a function, so you can effectively do `return $arr[key0][key1].....[keyN]`? – Marc B Aug 31 '15 at 20:51
  • 1
    Might be useful: http://stackoverflow.com/questions/27929875/how-to-write-getter-setter-to-access-multi-leveled-array-by-dot-separated-key-na – AbraCadaver Aug 31 '15 at 20:53
  • @Marc B, yes, i know you can provide a bunch of keys to directly access the array value, but what happen for example, in a function I want that value and at the same time validate its format and cast it to a certain type. Having a function that does all that would simplify considerably the code itself. That's why its necessary a function. – mikl Aug 31 '15 at 20:54
  • @AbraCadaver, i am not looking to process every array member, i just need to access one of its values. – mikl Aug 31 '15 at 20:56
  • Need more info, and are you talking about the `$_POST` array? – AbraCadaver Aug 31 '15 at 20:58
  • @user2864740, by not creatinga copy, i mean not wasting memory. I.e. without creating a copy of the array inside the function, since it is global. $_POST array is just an example, it can be any global array. – mikl Aug 31 '15 at 20:58
  • 2
    *There is no array / copy shown to be created in the example code*. Please make the code relevant. In any case, the general way to avoid a copy (on those types that do create copies, and where such *actually* matters) is to use references. Also note that PHP uses [*Copy on Write*](http://stackoverflow.com/questions/11074970/will-copy-on-write-prevent-data-duplication-on-arrays) for arrays so the 'expensive copy operation' is deferred until the copy is modified, if ever. – user2864740 Aug 31 '15 at 20:59
  • @user2864740, ok i added what would be the simplest way to traverse the array. Is it that simple as adding the `&` symbol after the assignment operator? – mikl Aug 31 '15 at 21:18
  • 1
    Much better after the updates. See http://repl.it/BEhl/4 - it basically is just using `&` in the assignment line; I propagate the returned value to also be a reference so the non-copy behavior can be seen. Is this any faster than the non-reference version? I honestly doubt it (but write a test to find out!).. and it's a heck of a lot more confusing. – user2864740 Aug 31 '15 at 21:46
  • @user2864740, nice, yours seems to be the right answer, i was thinking it had to do with recursive function, but passing by reference appears to simplify the problem a lot, and looks not confusing at all to me. Thanks – mikl Aug 31 '15 at 22:04

0 Answers0