0

I am using multidimensional arrays and am accessing them after using explode on a string. The resulting array should be nested with the number of occurrences of '.' in the given string. For instance, foo.bar.ok is ['foo']['bar']['ok'].

Currently, I am doing:

switch (count($_match)):
    case 1:
    $retVal = str_replace('{' . $match . '}', $$varName[$_match[0]], $retVal);
    break;
    case 2:
    $retVal = str_replace('{' . $match . '}', $$varName[$_match[0]][$_match[1]], $retVal);
    break;
    case 3:
    $retVal = str_replace('{' . $match . '}', $$varName[$_match[0]][$_match[1]][$_match[2]], $retVal);
    break;
endswitch;

Quintessentially I would like to have unlimited number of $_match[x] using a loop.

Edit The resulting array should be in the format: $array[foo][bar][ok]

Here are some examples I tried:

$string = 'foo.bar.ok';
$exploded = explode('.', $string);
$bracketed = array_map(function($x) { return [$x]; }, $exploded);
echo "<pre>";var_dump($bracketed);
$bracketed = array_map(function($x) { return "['$x']"; }, $_match);
$result = implode('', $bracketed);
var_dump(eval('return $t' . $result . ';'));

The first doesn't append arrays in nested structure, it lists them as 0, 1, 2, etc and the second works but it uses eval.

Finally, using loops as suggested worked.

for ($replace = $$varName[$_match[0]], $i = 1; $i < count($_match); $i++) {
    if (isset($replace[$_match[$i]]))
        $replace = $replace[$_match[$i]];
    }
    if (is_string($replace) || is_numeric($replace))
        $retVal = str_replace('{' . $match . '}', $replace, $retVal);

I would like to see a working array_walk example though? - thank you!

2
  • Please provide at least one example of what the $_match array will contain. Also, it's hard to determine what you're trying to do because your for() code does not do the same thing as your initial switch() code. The switch() code does a single str_replace() while the for() loop does multiple str_replace(), and also has checks for is_string() and is_numeric(). In any case, why don't you try the array_walk() method yourself and post the results if you have problems? Commented Jul 12, 2022 at 16:24
  • It's ok, I think the for loop is ok. The context is quite complicated the code is just an extract. Thank you all the same. Commented Jul 12, 2022 at 19:25

2 Answers 2

1

Use array_map() and implode().

$string = 'foo.bar.ok';
$exploded = explode('.', $string);
$bracketed = array_map(function($x) { return "[$x]"; }, $exploded);
$result = implode('', $bracketed);
Sign up to request clarification or add additional context in comments.

4 Comments

Ahh array_walk. Of course. The issue here was returning the array in a method call does not cause this behaviour. However, this does so hurrah. Many thanks.
sorry, got stuck again. $result returns [foo][bar][ok] but there is no array for me to use in PHP syntax. Doing eval('echo $t' . $result . ';'); works but this is a very strange approach? What am I missing?
What arrray are you talking about? If you have an array of foo.bar.ok-type strings you can execute this code in a foreach loop, array_map(), etc.
All sorted now, see main post thanks.
0

So you want to perform $$varName[$_match[0]][$_match[1]][$_match[2]...[$_match[count($_match)-1]?

for ($var_name = $varName[$_match[0]], $i=1; $i<count($_match); $i++) {
  $var_name = $var_name[$_match[$i]];
}
$retVal = str_replace('{' . $match . '}', $$var_name, $retVal);

This could probably be improved by replacing the for() with array_walk().

3 Comments

But $var_name would equal $var_name[$_match[2]]; not nested arrays as expected. Maybe I don't see something??
@youdsmedia Please edit your question to provide an example value for $_match so that we can duplicate the intended output.
Using this loop solved my problem.

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.