1

I have the following array structure (from a JavaScript object through JQuery AJAX):

[column_id]
    [column_content_id]
        [content]
            .....
        [options]
            .....
        [styles]
            .....

I go through the array with:

foreach($this->rom['columns'] as $column_id => $columns) {
    // add columns
    $this->add_column($column_id, $this->id, $columns['options']['column-width']);
    // content
    foreach($columns as $content_id => $content) {
        // assign values
        $values = array(
            'styles'        => $content['styles'], 
            'options'       => $content['options'],
            'style_class'   => 'is-cc-style', 
            'in_column'     => true, 
            'id'            => str_replace('#', '', $mc_content_id),
            'column_id'     => $column_id,
            'content_type'  => $content['content_type'],
            'options_class' => 'is-cc-option'
        );
    }
}

Everything works well, except that I receive the following notices:

Notice: Undefined index: styles
Notice: Undefined index: options
Notice: Undefined index: content_type

How can I avoid this? At the moment I use:

if(isset($content['styles'])) {
    $content['styles'] = '';
}

But I guess that's not how it's supposed to be.

Thanks for any help! :)

2
  • Is the lack of a closing ] on this line if(isset($content['styles')) { a typo? Commented Jul 25, 2014 at 19:49
  • yes sorry i edited it, it was just a copy paste problem, its correct in the php file Commented Jul 25, 2014 at 19:52

2 Answers 2

2

It looks like you want to return an empty string if the property is not set, and what you're using looks exactly like a syntax error to me ?

Here's how I'd do it

    $values = array(
        'styles'        => isset($content['styles']) ? $content['styles'] : "", 
        'options'       => isset($content['options']) ? $content['options'] : "",
        'style_class'   => 'is-cc-style', 
        'in_column'     => true, 
        'id'            => str_replace('#', '', $mc_content_id),
        'column_id'     => $column_id,
        'content_type'  => isset($content['content_type']) ? $content['content_type'] : "",
        'options_class' => 'is-cc-option'
    );
Sign up to request clarification or add additional context in comments.

Comments

0

You will get notices like this sometimes in php when you try to assign something that is NULL to a variable (it depends on the type of errors you have turned on), I am guessing that styles, options and or content_type are nullable? If so you can use an @ to suppress the warning like so:

    $values = array(
        'styles'        => @$content['styles'], 
        'options'       => @$content['options']
    );

or if you want to set it to a default value adeneo's solution would be best.

Comments

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.