0

I am having a problem appending a few options to an array of modules. I am using Opencart and trying to extend a module by adding an image. To do this and ensure that the code will not break anything in the future I wanted to add to the array instead of replace it.

This is the code I have so far:

if (isset($this->request->post['special_module'])) {
    $modules = $this->request->post['special_module'];
} elseif ($this->config->get('special_module')) { 
    $modules = $this->config->get('special_module');
}

$this->load->model('tool/image');

foreach ($modules as $module) {
    if (isset($module['image']) && file_exists(DIR_IMAGE . $module['image'])) {
        $image = $module['image'];
    } else {
        $image = 'no_image.jpg';
    }           

    array_push($module, array(
        'image'        => $image,
        'thumb'        => $this->model_tool_image->resize($image, 100, 100)
    )); 
} 
print_r($modules);exit;
$this->data['modules'] = $modules;

Print Array, no image or thumb:

Array
(
    [0] => Array
        (
            [image_width] => 307
            [image_height] => 234
            [layout_id] => 1
            [position] => column_right
            [status] => 1
            [sort_order] => 1
        )

)

When I do array_push do I need to assign this back to the array?

3 Answers 3

2

$module is being overwritten by the foreach() loop every time it iterates. So your push is basically a null-op, becaus foreach will destroy the previous $module (that you pushed to) with the next $module value coming out of $modules. You'd need something more like this:

foreach($modules as &$module) {
    ...
    $module['image'] = $image;
    $module['thumb'] = ...;
}

The & before $module in the foreach turns it into a reference, so any modifications to $module within the loop will modify the original element in $modules, rather than a copy which would get trashed on every iteration.

Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant, there is so much I still have to learn as PHP. Are there any books/guides you would recommend to learn more about references? When I do a Google search all I can find it actual references to PHP.
Most online stuff you'll find for php tutorials is badly out of date (talking about PHP 4 and early 5s), hideously crappy in quality, and/or flat-out wrong. Especially avoid anything from w3fools. The PHP site is actually pretty good, and the comments in the function reference section are frequently quite helpful too.
1

$module, in your foreach loop is a copy of the contents. You will need to access it by reference, or push back into the actual array $modules.

Try modifying the foreach signature to the following:

foreach ($modules as &$module) {

Comments

0

try using array_merge instead of array_push

array_merge($module, array(
    'image'        => $image,
    'thumb'        => $this->model_tool_image->resize($image, 100, 100)
)); 

edit:

also, as print_r outputs the correct should be array_merge($module[0], array(...));

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.