Accessing index of associative array with variable
Posted: Wed Nov 26, 2008 12:09 pm
Hello,
I am a bit new to php. I am trying to access an objects data members with a variable name. Here is a quick example to demonstrate my problem:
My goal is to have the above print out 2, but instead it prints out
How would I be able to use a variable name to access the index of an associative array?
I am a bit new to php. I am trying to access an objects data members with a variable name. Here is a quick example to demonstrate my problem:
Code: Select all
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$testme = new Test();
$testme->printTest("test2");
class Test
{
public $example = array("test1"=>1, "test2"=>2, "test3"=>3);
function printTest($testNumber)
{
echo $this->$testNumber;
}
}
?>
My goal is to have the above print out 2, but instead it prints out
Code: Select all
Notice: Undefined property: Test::$test2