Re: Cannot use string offset as an array



Tree*Rat wrote:
Why do i get an error on this line?

if( $this->$switch_b[$i]['module_id'] == $id )

it says: Fatal error: Cannot use string offset as an array in D:\htdocs\******
\includes\classes\modules.php on line 307



full code:


function get_module_data($switch, $id)
{
$switch_b = 'info_' . $switch;

for( $i = 0; $i < count($this->$switch_b); $i++ )
{
LINE 307-> if( $this->$switch_b[$i]['module_id'] == $id )
{
$arr = $this->$switch_b[$i];

foreach( $arr as $key => $value )
{
$this->mod_data[$key] = $value;
}
}
}

if( empty($this->mod_data) )
{
message_handler(GENERAL_MESSAGE, 'Module data not found');
}

return $this->mod_data;
}


thanks


then you use $this-> to access a variable in a class you do not need any more additional '$'

example
class test
{
var $setstuff;

function dostuff( $stuff)
{
$this->setstuff=$stuff;
}
}

However if you are NOT using a class the variables are internal to the function so you don't need to use $this-> in your function, it needs to look like

function get_module_data($switch, $id)
{
$switch_b = 'info_' . $switch;

for( $i = 0; $i < count($switch_b); $i++ )
{
if( $switch_b[$i]['module_id'] == $id )
{
$arr = $switch_b[$i];

foreach( $arr as $key => $value )
{
$mod_data[$key] = $value;
}
}
}

if( empty($mod_data) )
{
message_handler(GENERAL_MESSAGE, 'Module data not found');
}

return $mod_data;
}
.