0

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.

2
  • Without context of the CProject object, it's kind of hard to pinpoint what exactly is going on. Commented Feb 10, 2016 at 17:34
  • You don't have keys named project_name in $proj, so $p['project_name'] is invalid. Just do $project_select_list[$k] = $p; Commented Feb 10, 2016 at 17:35

1 Answer 1

4

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @RocketHazmat for notice the bracket outside :)
Getting rid of ['project_name'] and just using $p did the trick as Sean and Rocket both suggested. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.