CodeIgniter Forums

Full Version: Array General Question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
When I use [] with my code below


Code:
$data['modules'][] = array (
  'module' =>  $this->$part[0]($setting_info),
);

It makes the module throw a error saying array string conversion error.

With out [] like below makes work?

Code:
$data['modules'] = array (
'module' =>  $this->$part[0]($setting_info),
);

[Image: with_out_error.png]

[b]With [] on $data['modules'][] image not working error

[Image: with_error.png]
[/b]

Question Why can I not use [] with my [b]$data['modules'][][/b]


PHP Code:
<?php

class Home extends CI_Controller {

    public function 
__construct() {
        
parent::__construct();
        
$this->load->model('design/model_layout');
        
$this->load->model('extension/model_module');
        
$this->load->model('design/model_banner');
        
$this->load->model('tool/model_image');

        
$this->load->library('image_lib');
    }

    public function 
index() {
        
$data['title'] = 'Home';
        
$data['column_left'] = $this->column_left();
        
$data['content_top'] = $this->content_top();
        
$data['page'] = 'common/home';

        
$this->load->view('common/template'$data);
    }


    public function 
column_left() {
        if (
$this->uri->segment(1)) {
 
           $route $this->uri->segment(1) .'/'$this->uri->segment(2);
 
       } else {
 
           $route 'common/home';
 
       }
        
        
$layout_id $this->model_layout->get_layout($route);

        
$data['modules'] = array();

        
$modules $this->model_layout->get_layout_modules($layout_id'column_left');

        foreach (
$modules as $module) {
            
$part explode('.'$module['code']);
            
            
$setting_info $this->model_module->get_module($part[1]);

            
$data['modules'] = array (
                
'module' =>  $this->$part[0]($setting_info),
            );
        }

        return 
$this->load->view('common/column_left'$dataTRUE);
    }

    public function 
content_top() {
        if (
$this->uri->segment(1)) {
 
           $route $this->uri->segment(1) .'/'$this->uri->segment(2);
 
       } else {
 
           $route 'common/home';
 
       }
        
        
$layout_id $this->model_layout->get_layout($route);

        
$data['modules'] = array();

        
$modules $this->model_layout->get_layout_modules($layout_id'content_top');

        foreach (
$modules as $module) {
            
$part explode('.'$module['code']);
            
            
$setting_info $this->model_module->get_module($part[1]);

            
$data['modules'] = array (
                
'module' =>  $this->$part[0]($setting_info),
            );
        }

        return 
$this->load->view('common/content_top'$dataTRUE);
    }

    public function 
slideshow($setting_info) {
        static 
$module 0;

 
       $data['banners'] = array();

 
       $results $this->model_banner->get_banner($setting_info['banner_id']);

 
       foreach ($results as $result) {
 
           $data['banners'][] = array(
 
               'banner_image' => $this->model_image->resize($result['banner_image'], $setting_info['width'], $setting_info['height'])
 
           );
 
       }

 
       $data['module'] = $module++;

        return 
$this->load->view('module/slideshow'$dataTRUE);
    }

    public function 
category() {

        
$this->load->model('page/model_page_category');

        
$uri_3 $this->uri->segment(3);

        if (
$uri_3) {
            
$data['category_id'] = $uri_3;
        } else {
            
$data['category_id'] = 0;
        }

        
$uri_4 $this->uri->segment(4);

        if (
$uri_4) {
            
$data['child_id'] = $uri_4;
        } else {
            
$data['child_id'] = 0;
        }

        
$data['categories'] = array();

        
$categories $this->model_page_category->get_categories(0);

        foreach (
$categories as $category) {

            
$children_data = array();

            if (
$category['category_id'] == $data['category_id']) {

                
$children $this->model_page_category->get_categories($category['category_id']);

                foreach(
$children as $child) {

                    
$children_data[] = array(
                        
'category_id' => $child['category_id'],
                        
'name' => $child['name'],
                        
'href' => $this->config->item('url') . 'page/categories/' $category['category_id'] . '/' $child['category_id']
                    );
                }
            }

            
$data['categories'][] = array(
                
'category_id' => $category['category_id'],
                
'name' => $category['name'],
                
'children' => $children_data,
                
'href' => $this->config->item('url') . 'page/categories/' $category['category_id']
            );
        }


        
$data['page'] = 'module/category';

        return 
$this->load->view('common/template'$dataTRUE);
    
    }

(10-22-2015, 12:03 AM)riwakawd Wrote: [ -> ]When I use [] with my code below



Code:
$data['modules'][] = array (
  'module' =>  $this->$part[0]($setting_info),
);

It makes the module throw a error saying array string conversion error.

With out [] like below makes work?


Code:
$data['modules'] = array (
'module' =>  $this->$part[0]($setting_info),
);

[Image: with_out_error.png]

[b]With [] on $data['modules'][] image not working error

[Image: with_error.png]
[/b]

Question Why can I not use [] with my [b]$data['modules'][][/b]



PHP Code:
<?php

class Home extends CI_Controller {

 public function 
__construct() {
 
parent::__construct();
 
$this->load->model('design/model_layout');
 
$this->load->model('extension/model_module');
 
$this->load->model('design/model_banner');
 
$this->load->model('tool/model_image');

 
$this->load->library('image_lib');
 }

 public function 
index() {
 
$data['title'] = 'Home';
 
$data['column_left'] = $this->column_left();
 
$data['content_top'] = $this->content_top();
 
$data['page'] = 'common/home';

 
$this->load->view('common/template'$data);
 }


 public function 
column_left() {
 if (
$this->uri->segment(1)) {
 
           $route $this->uri->segment(1) .'/'$this->uri->segment(2);
 
       } else {
 
           $route 'common/home';
 
       }
 
 
$layout_id $this->model_layout->get_layout($route);

 
$data['modules'] = array();

 
$modules $this->model_layout->get_layout_modules($layout_id'column_left');

 foreach (
$modules as $module) {
 
$part explode('.'$module['code']);
 
 
$setting_info $this->model_module->get_module($part[1]);

 
$data['modules'] = array (
 
'module' =>  $this->$part[0]($setting_info),
 );
 }

 return 
$this->load->view('common/column_left'$dataTRUE);
 }

 public function 
content_top() {
 if (
$this->uri->segment(1)) {
 
           $route $this->uri->segment(1) .'/'$this->uri->segment(2);
 
       } else {
 
           $route 'common/home';
 
       }
 
 
$layout_id $this->model_layout->get_layout($route);

 
$data['modules'] = array();

 
$modules $this->model_layout->get_layout_modules($layout_id'content_top');

 foreach (
$modules as $module) {
 
$part explode('.'$module['code']);
 
 
$setting_info $this->model_module->get_module($part[1]);

 
$data['modules'] = array (
 
'module' =>  $this->$part[0]($setting_info),
 );
 }

 return 
$this->load->view('common/content_top'$dataTRUE);
 }

 public function 
slideshow($setting_info) {
 static 
$module 0;

 
       $data['banners'] = array();

 
       $results $this->model_banner->get_banner($setting_info['banner_id']);

 
       foreach ($results as $result) {
 
           $data['banners'][] = array(
 
               'banner_image' => $this->model_image->resize($result['banner_image'], $setting_info['width'], $setting_info['height'])
 
           );
 
       }

 
       $data['module'] = $module++;

 return 
$this->load->view('module/slideshow'$dataTRUE);
 }

 public function 
category() {

 
$this->load->model('page/model_page_category');

 
$uri_3 $this->uri->segment(3);

 if (
$uri_3) {
 
$data['category_id'] = $uri_3;
 } else {
 
$data['category_id'] = 0;
 }

 
$uri_4 $this->uri->segment(4);

 if (
$uri_4) {
 
$data['child_id'] = $uri_4;
 } else {
 
$data['child_id'] = 0;
 }

 
$data['categories'] = array();

 
$categories $this->model_page_category->get_categories(0);

 foreach (
$categories as $category) {

 
$children_data = array();

 if (
$category['category_id'] == $data['category_id']) {

 
$children $this->model_page_category->get_categories($category['category_id']);

 foreach(
$children as $child) {

 
$children_data[] = array(
 
'category_id' => $child['category_id'],
 
'name' => $child['name'],
 
'href' => $this->config->item('url') . 'page/categories/' $category['category_id'] . '/' $child['category_id']
 );
 }
 }

 
$data['categories'][] = array(
 
'category_id' => $category['category_id'],
 
'name' => $category['name'],
 
'children' => $children_data,
 
'href' => $this->config->item('url') . 'page/categories/' $category['category_id']
 );
 }


 
$data['page'] = 'module/category';

 return 
$this->load->view('common/template'$dataTRUE);
 
 }


View data will be passed to extract() function. Maybe following examples will help you:

PHP Code:
$data['modules'][] = array('module' => 'value');
extract($data);
var_dump($modules);
// result
array(1) {
  [0]=>
  array(1) {
    ["module"]=>
    string(5"value"
  }
}
// access $modules value
echo $modules[0]['module']; 


PHP Code:
$data['modules'] = array('module' => 'value');
extract($data);
var_dump($modules);
// result
array(1) {
  ["module"]=>
  string(3"value"
}
// access $modules value
echo $modules['module'];