Search code examples
phpundefinedopencart

Error : Getting "Undefined array key" whule running my application in open cart


MY CODE :

 $data['components'][] = array(
                        'id_component' => $component['id_component'],
                        'id_layout' => $component['id_layout'],
                        'component_type' => $this->model_webservice_webservice->getComponentTypeByID($component['id_component_type']),
                        'component_heading' => @$component['component_heading'],
                        'data' => $component_data,
                        'product_data' => $products
                    );

ERROR LINE : 'component_heading' => @$component['component_heading'],

ERROR : Undefined array key "component_heading" in C:\xampp\htdocs***\extension\module\webservice.php on line 1870Warning:


Solution

  • Avoid using Error Suppression operator (it's behaviour has changed since PHP8.

    You can use one of following syntaxes when there is no such key:

    'component_heading' => $component['component_heading'] ?? null, // null coalesce operator
    'component_heading' => isset($component['component_heading']) ? $component['component_heading'] : null,
    'component_heading' => array_key_exists('component_heading', $component) ? $component['component_heading'] : null,