mckinnej
mckinnej

Reputation: 33

PHP Illegal String Offset Warning

Disclaimer: I'm not a PHP programmer. I don't really consider myself a programmer at all. I was just unfortunate enough to inherit some code that doesn't work.

So I have this code. I believe it was written for PHP4, but we're using PHP5.

// Retrieve project list
$project = new CProject();
$proj = $project->getAllowedRecords( $AppUI->user_id, 'project_id, project_name');
foreach ( $proj as $k => $p ) {
    $project_select_list[$k] = $p['project_name'];
}

This is supposed to populate a pick list based on the user's role, but it doesn't work. I get the "illegal string offset" warning on the line inside of the foreach loop. I think understand what it's trying to do, but I don't enough about PHP to fix it. I did a vardump on $proj and it returned this.

array(5) { 
  [1]=> string(4) "Roof" 
  [2]=> string(4) "Wall" 
  [3]=> string(4) "Yard" 
  [4]=> string(7) "Kitchen" 
  [5]=> string(8) "Bathroom" 
} 

Anyone got any hints on how to fix it? Thanks.

Upvotes: 0

Views: 2327

Answers (1)

Marcos Pérez Gude
Marcos Pérez Gude

Reputation: 22158

This seems to be produced by this:

 $p['project_name'];

If $p variable is a string (as the var_dump() said), you don't have an array to access nowhere. The most probably solution is this:

foreach ( $proj as $k => $p ) {
    $project_select_list[$k] = $p;
}

Upvotes: 4

Related Questions