Codeigniter: Pass Multidimensional Array to the View

In this post, we will see how to pass multidimensional array to the view in codeigniter.Multidimensional array is an array which contains more than one array. Suppose we have two table one is employee basic details and the other is salary table and we want data from both table in an array. So the best way to produce all these data in a clean manner by using multidimensional array.

Lets have an example of multidimensional array.

Controller:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MultidimensionalArrayController extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
      }
	public function index()	{ 
        $data = array();
        $data['employeedetails']['basicinfo']['name'] = "Ajay Kumar";
        $data['employeedetails']['basicinfo']['contact']['phone'] = "0657-2246748";
        $data['employeedetails']['basicinfo']['contact']['mobile'] = "8645697412";
        $data['employeedetails']['basicinfo']['email'] = "ajay@gmail.com";
        $data['employeedetails']['basicinfo']['address'] = "Bagbera,Jamshedpur";
        $data['employeedetails']['salaryinfo']['bank'] = "ICICI";
        $data['employeedetails']['salaryinfo']['acno'] = "18556464846484";
        $data['employeedetails']['salaryinfo']['branch'] = "Jamshedpur";
        $data['employeedetails']['salaryinfo']['ifsc'] = "ICIC864";
        $this->load->view('multidimensional', $data);
	}
}

View:

<html>
<head>
<title>Multidimensional Array Example</title>
</head>
<body>
<style>
h3{
  font-family: Verdana; 
  font-size: 18pt;         
  font-style: normal; 
  font-weight: bold; 
  color:red;
  text-align: center; 
}
table{
  font-family: Verdana; 
  color:black; 
  font-size: 12pt; 
  font-style: normal;
  font-weight: bold;
  text-align:left; 
  
}
</style>
<h3>Multidimensional Array Example</h3>
<div class="row">
    <div class="col-md-2"></div>
    <div class="col-md-8">
	<table id="table" align="center" cellpadding = "5" border="2" class="table table-striped table-bordered" cellspacing="0" width="80%" style="position:relative; top:7px;">
        <thead>
        <tr><th colspan="4">Basic Information</th></tr>
        <tr>
            <th>Name</th>
            <th>Contact</th>
            <th>Email</th>
            <th>Address</th>
        </tr>
        </thead>
        <tbody>
            <tr>
                <td><?php echo $employeedetails['basicinfo']['name']; ?></td>            
                <td>Phone: <?php echo $employeedetails['basicinfo']['contact']['phone']; ?> | Mobile: <?php echo $employeedetails['basicinfo']['contact']['mobile']; ?></td>
                <td><?php echo $employeedetails['basicinfo']['email']; ?></td>
                <td><?php echo $employeedetails['basicinfo']['address']; ?></td>
            </tr>
       
        </tbody>
    </table>
    <table id="table" align="center" cellpadding = "5" border="2" class="table table-striped table-bordered" cellspacing="0" width="80%" style="position:relative; top:7px;">
        <thead>
        <tr><th colspan="4">Salary Information</th></tr>
        <tr>
            <th>Bank</th>
            <th>A/c No.</th>
            <th>Branch</th>
            <th>IFSC</th>
        </tr>
        </thead>
        <tbody>
            <tr>
                <td><?php echo $employeedetails['salaryinfo']['bank']; ?></td>            
                <td>Phone: <?php echo $employeedetails['salaryinfo']['acno']; ?></td>
                <td><?php echo $employeedetails['salaryinfo']['branch']; ?></td>
                <td><?php echo $employeedetails['salaryinfo']['ifsc']; ?></td>
            </tr>
       
        </tbody>
    </table>
    </div>
    <div class="col-md-2"></div>
</div> 
</body>
</html>
Multidimensional Array
Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion. if you face any issue with this code let us know. We’d love to help!

I am Hitesh from Jamshedpur (India). I have been working as a Web Application Developer from last 4+ years. I love diverse and attention catching web presence for a variety of users. Also I love to learn new things in Web Development.

Tags: , , ,