Home  >  Q&A  >  body text

codeigniter - Generate multi-dimensional array in Controller, how to call it in view

Generally we use one-dimensional arrays, for example:
$data['regions'] = $this->index_model->get_region();
Then use $regions directly in the view to make data calls.
But what if I want to generate a multidimensional array, for example:
$data[1]['streets'] = $this->index_model->get_street(1,1);
$data[2]['streets'] = $this->index_model->get_street(2,1);
In this way, how to call it in the view, thank you everyone~

PHP中文网PHP中文网2713 days ago345

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 16:45:40

    Place the contents of a multidimensional array into an element of the array passed to the $this->load->view() method, like this:

    $data['streets'][1]['streets'] = $this->index_model->get_street(1,1);
    $data['streets'][2]['streets'] = $this->index_model->get_street(2,1);
    

    Then pass the $data array to the $data数组传递给$this->load->view() method, like this:

    $this->load->view('...', $data);
    

    After that you can call it like this in the view:

    <?php echo $streets[1]['streets']; ?>
    <?php echo $streets[2]['streets']; ?>
    

    reply
    0
  • Cancelreply