Why do I get "Undefined variable" when trying to resolve a multi-dimensional array?

Der Hochstapler

I'm trying to retrieve an item from a multi-dimensional array through a string that describes the path into the array (like first.second.third).

I've chosen the approach as shown here (also available on ideone):

<?php
    // The path into the array
    $GET_VARIABLE = "a.b.c";
    // Some example data
    $GLOBALS["a"]= array("b"=>array("c"=>"foo"));

    // Construct an accessor into the array
    $variablePath = explode( ".", $GET_VARIABLE );
    $accessor = implode( "' ][ '", $variablePath );
    $variable = "\$GLOBALS[ '". $accessor . "' ]";

    // Print the value for debugging purposes (this works fine)
    echo $GLOBALS["a"]["b"]["c"] . "\n";
    // Try to evaluate the accessor (this will fail)
    echo $$variable;
?>

When I run the script, it will print two lines:

foo
PHP Notice:  Undefined variable: $GLOBALS[ 'a' ][ 'b' ][ 'c' ] in ...

So, why does this not evaluate properly?

RandomSeed

I think the $$ notation only accepts a variable name (ie. the name of a variable). You are actually trying to read a variable named "$GLOBALS[ 'a' ][ 'b' ][ 'c' ]", which does not exist.

As an alternative ($GET_VARIABLE seems to be your input string), you could try this:

$path = explode('.', $GET_VARIABLE);
echo $GLOBALS[$path[0]][$path[1]][path[2]];

Wrap this in a suitable loop to make it more dynamic; it seems to be trivial.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do you get the width and height of a multi-dimensional array?

How do I get the dimensions of multi-dimensional arrays?

how do i correctly handle a multi dimensional numpy array

Why do I an get error when trying to print a null pointer

Why do I get "Undefined variable" errors with pseudo-selectors in SASS?

Why do you not start the definition of an multi-dimensional array at 0?

Why do I get a segmentation fault when trying to write a graph?

How do I return such an multi-dimensional array?

Why do I get square brackets when trying to convert array to JSON string using stringify

Why do i get this error when trying to check if in Corona sdk?

Why do I get SIGABRT when trying to addObject to NSMutableArray

Why do I get "Undefined variable" in my custom PHP function

Why do I get an undefined variable error when trying to use PHP in two sections of a page?

Why do I get TypeError when trying to do dot product?

How do I find maximum value in a multi dimensional array

How do I get values inside this multi-dimensional array

Why do I get undefined variable in an if statement?

How do I trim '\n' in multi dimensional array in php

Why do I get "Cannot resolve symbol" when trying to use class from library?

Why do I get error when trying to add file attachments?

Why i get Undefined variable when i use PostController in Laravel?

How do I return such an multi-dimensional array

Why do I get this error with phpmailer when trying to send an email?

Why do I get a undefined variable warning when using a class and construct?

Why do I get an Undefined Variable: request in laravel

Why do I get a segfault when trying to access members of struct?

Why do I get an Undefined reference error when trying to compile?

Laravel, Undefined Variable when trying to pass array from controller to views

Why do I get InvalidDocumentContent when trying to create a SSM document?