0

During the execution all errors are saved into an array like this:

$this->errors[$section] = $e->getMessage();

The thing is that, after the execution, I have to publish all sections included the ones that experimented an error concatenated with the error. When I try to to:

echo $this->errors[$section];

this is the warning I get:

Warning: Illegal offset type in ....

The solution described at Dynamic access to a PHP array didn't help.

1
  • You want to get all of the messages in the array? Or just a specific one? Commented Aug 10, 2012 at 20:42

2 Answers 2

1

Check to see if the index is valid before using it:

if (array_key_exists($section, $this->errors) ) { 
    echo $this->errors[$section]; 
}
Sign up to request clarification or add additional context in comments.

Comments

1

IF you haven't set $section, then PHP won't know what to return.

Assuming that your sections are arrays have some sort of ID number or string:

$errors[$section['id']] = $e->getMessage();
...
foreach($errors as $error){
    print_r($error);
}

I have a feeling that your problem is in the fact that $section is no longer set to what you want in the second code block.

7 Comments

I didn't - I saw you removed your comment and I wouldn't want to steal rep.
There's no indication in OP's question that $section is an array.
I take that back. It's the offset type that's illegal, and therefore may be due to $section having been defined as an array. You may be on to something here.
I think it's that $section isn't set properly in the second portion of the code, so OP needs to have some sort of ID to reference it properly.
Yes and no; OP just needs to make sure that its value is what he thinks it is before trying to access the index at value $section.
|

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.