Multidimensional array in a php class

I am trying to declare and use a multidimensional array in a php class and it’s not working.

The primary declaration is

var $retvals = array();

I want there to be 5 sub arrays (“field”, “fname”, “ftitle”, “fshow”, “finfo”)

and then I want to put multiple indexed values in each so I can assign like

$this->retvals[“field”][0] = var;
$this->retvals[“field”][1] = var;

I’ve tried different combinations of declaration and assignment and none seems to work.

Your construct works perfectly. Actually I ended up with

public function __construct() {
    $this->retvals = array(
        'field' => array(array()),
        'fname' => array(),
        'ftitle' => array(),
        'fshow' => array(),
        'finfo' => array()
    );
}

because the field array actually is multiple records, multiple fields within the field array. Thanks so much for the help and although I’m not sure why or why it didn’t work before, I suppose it’s not critical. This is definitely a better way to go. Thanks again.

Sorry poop, it’s inside a function in a class. My understanding is when you declare a variable at the class level and use it inside the function, you have to clarify it with the $this referring to the class assignment.

Very strange, although I’m sure there is an explanation. If I assign it first to a simple variable and then assign it to the retvals array, it works:

for ($x = 0; $x < ($this->numcols); $x++)
{
$this->foo = $this->user[$this->colname[$x]];
$this->retvals[“field”][$x] = $this->foo;
}
I have seen this with $_SESSION variables as well. You can’t assign a complex statement to them directly, you have to go to a simple variable and then to the array. If that is correct or anyone has any input on that I would love to hear it.

you are setting it as $retvals at the top and then referencing it as $this.retvals later…remove “this.”

I will try that, but did you see my comments above. It does let me set it at the class level if I do the assignment to the simple variable $foo first and then from foo the the array. It works fine that way.

So to be clear, I am now declaring the multidimensional array at the class level and then doing the assignment in the function, but the assignment goes to $foo first and then to the $this->retvals[‘field’][$x] array element. Works like a charm. Should it not? I would love to understand exactly what’s going on here.

Also, on your last example, you declared your functions public. Why? Sorry I don’t know the difference between public and non-public at this point.

That doesn’t work, here is the actual code for the declaration and the assignment. It gives me the error – Parse error: syntax error, unexpected ‘[’ in on the assignment line.

var $retvals = array(
‘field’ => array(),
‘fname’ => array(),
‘ftitle’ => array(),
‘fshow’ => array(),
‘finfo’ => array()
); //N - return array of column values

foreach ($this->users as $this->user)
{
for ($x = 0; $x < ($this->numcols); $x++)
{
$this.retvals[‘field’][$x] = $this->user[$this->colname[$x]];
}
}

there is code in between obviously but that’s not the issue, I even removed the argument and replaced it with 5 and I still get the same error pointing to the assignment line so there is something going on with the retvals creation.

ah sorry, i didn’t realize you were setting the variable in the class. you won’t be able to set it to a multi dimensional array when you define it, but you can when the class is instantiated

does this work for you?
http://poundpipe.com/v/baaal/multi-dimensional-array-class-version

I made it public because I wasn’t sure how you were using it. If you’re just accessing the variable from within the class, you can make it private. (not specifying will default to public)

i think you’re looking for something like this

http://poundpipe.com/v/baaak/multi-dimensional-array

Thanks for all your help. I have it working great.

Sorry, wrong terminology. It’s inside a method of the class. :slight_smile: