3

I can use $$ variables to refer variable like this

$var = 'car';
$car = 'Lamborghini';
echo $$var;

Above code will echo Lamborghini.

However I am having a code like this:-

$var = "['acct_1']['etc']['anotherInfo']['sing']";
$var = 'arr'.$var;
echo $arr['acct_1']['etc']['anotherInfo']['sing'] ;
echo $$var;

First echo prints the correct value but $$var doesn't give the correct value.

Any help is much appreciated.

Thanks

11
  • 4
    This obviously doesn't work with arrays Commented May 29, 2015 at 12:59
  • OK, I will close the question but not able to find the option to do so. Commented May 29, 2015 at 13:00
  • 3
    why, let it be. check for sometime. Commented May 29, 2015 at 13:02
  • OK, Probably some work around might be suggested by some one. Commented May 29, 2015 at 13:03
  • 1
    it's not array it's string man. Commented May 29, 2015 at 13:10

2 Answers 2

2

You can always keep the keys in an array, and then iterate on them to resolve the value correctly:

$keys = ['acct_1', 'etc', 'anotherInfo', 'sing'];

$val = $arr;
foreach($keys as $key) {
    $val = $val[$key];
}

Now, both $arr['acct_1']['etc']['anotherInfo']['sing'] and $val have the same value.

Try it in this demo.

Edit:

You already have the $keys array in $indexInfo. You should be able to use it like so:

function replaceValue($arr, $indexInfo, $char)
{
    // $indexInfo is all you need!
    $var = $arr;
    foreach($indexInfo as $key) {
        $var = $var[$key];
    }
    echo $arr['acct_1']['etc']['anotherInfo']['sing'] . "\n";
    echo $var  . "\n";
    die($var);
}
Sign up to request clarification or add additional context in comments.

8 Comments

$arr = array( 'write' => 100, 'acct_1' => array('blah' => 'foo', 'dance' => 24), 'acct_1' => array('type' => 'string', 'length' => 5, 'etc' => array('anotherInfo' => array( 'info1' => '', 'sing' => 500000000 ))), ); Lets say we have array like this , and now you want to refer sing based on some calculations. So what I am doing is storing the traversed values in an array and then generating the string mentioned in question.
@JatinDhoot - When you say "storing the traversed values in an array", aren't you forming the exact same array that I called $keys in my answer? In which case, you should be able to use that array to implement my answer.
@JatinDhoot :you said, you are imploding the keys, instead keep those in the array form and apply his logic, Its working fine. Tested with the sample you provided in the above comment
OK GUys, let me havea look
@nickb - Mine is multi level nested array, and dimensions are not known in advance
|
1

That won't work unfortunately, however why not do something line this

/**
 * Search into a multi dimensional array to find arbitrary data
 * @param array $array The array to search
 * @param string ... Any number of array keys
 * @return mixed
 */
function deepArraySearch(array $array) {
    $keys = func_get_args();
    array_shift($keys); // First element is the array

    // If no more keys to use
    if(!$keys) {
        return $array;
    }

    $nextKey = array_shift($keys);
    $nextData = $array[$nextKey];

    // Nothing left to search
    if(!is_array($nextData )) {
        return $nextData ;
    }

    array_unshift($keys, $nextData);
    return call_user_func_array('deepArraySearch', $keys);
}

$arr = ['one' => ['two' => ['three' => 'data']]];

print_r(deepArraySearch($arr, 'one'));
print_r(deepArraySearch($arr, 'one', 'two'));
print_r(deepArraySearch($arr, 'one', 'two', 'three'));

echo PHP_EOL;

In your case I guess it would work like this

$arr = ['acct_1' => ['etc' => ['anotherInfo' => ['sing' => 'song']]]];
print_r(deepArraySearch($arr, 'acct_1', 'etc', 'anotherInfo', 'sing')); // song

Final note:

If you're using PHP 5.6, 7, or HHVM, this function is way nicer:

<?php

/**
 * Search into a multi dimensional array to find arbitrary data
 * @param array $array The array to search
 * @param string ... Any number of array keys
 * @return mixed
 */
function deepArraySearch(array $array, ...$keys) {

    // If no more keys to use
    if(!$keys) {
        return $array;
    }

    $nextKey = array_shift($keys);
    $nextData = $array[$nextKey];

    // Nothing left to search
    if(!is_array($nextData )) {
        return $nextData ;
    }

    return deepArraySearch($nextData, ...$keys);
}

Demo: http://3v4l.org/vmocO

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.