How to Handle Multidimensional Array in Ajax through PHP(code igniter) and Jason

json_decode multidimensional array php
php json_encode multidimensional array
create multidimensional json array in php
how to get data from json array in php
json to array php
php json foreach key => $value
json multidimensional array java
php loop through json array

Here is the Html where i want to display data from database using ajax

<div id="specContWrap">
  <div id="overViewWrap" class="overViewTableCon">
    <div class="overViewTable on">
      <div class="tit">Overview</div>
      <table class="infoTableType">
        <caption>Overview Table</caption>
        <tbody id="overview_details">
          <tr>
            <th scope="row">Category</th>
            <td> Commercial</td>
          </tr>
          <tr>
            <th scope="row">Product label</th>
            <td> Select Trim </td>
          </tr>
          <tr>
            <th scope="row">Model name</th>
            <td> H-1</td>
          </tr>
          <tr>
            <th scope="row">Seats</th>
            <td>Select Trim</td>
          </tr>
        </tbody>
      </table>
    </div>
    <!-- //.overViewTable -->
  </div>
  <!-- toggle : s -->
  <div class="togListWrap small  sepcListWrap" id="overviewh1">
    <div class="togList">
      <button class="btnOpenClose ">Performance</button>
      <div class="togCont " id="performanceWrap">
        Select Trim
      </div>
    </div>
    <div class="togList">
      <button class="btnOpenClose ">Dimension</button>
      <div class="togCont " id="dimensionWrap">
        <div class="dimension on">
          Select Trim
        </div>
        <!-- //demention -->
      </div>
    </div>
    <div class="togList">
      <button class="btnOpenClose ">Wheels</button>
      <div class="togCont " id="wheelsWrap">
        <div class="wheels on">
          Select Trim
        </div>
        <!-- //wheels -->
      </div>
    </div>
  </div>
</div>
<div id="htmSpecContWrap" class="hide">
</div

Here is the j Query on change Function

$(document).ready(function() {
  $('#selTrim').change(function() {
    var trim_id1 = $(this).val();
    if (trim_id1) {
      $.ajax({
        type: 'POST',
        url: '<?= base_url();?>H1_specifications/h1_trim_detail',
        data: 'trim_id1=' + trim_id1,
        datatype: 'Jason',
        success: function(data) {
          console.log(JSON.stringify(data));
          // here i want to Display data up in the html,
          //which had responded through controller.
        }
      });
    }
  });
});

Here The controller function

public function h1_trim_detail(){
    $id = $_POST['trim_id1'];
    if(isset($_POST['trim_id1']) !== NULL){
        $this->load->model('Admin_panel');

        $results['overview_details'] =$this->Admin_panel->select_h1_details_specification($id);
        $results['cargo_details'] = $this->Admin_panel->select_h1_Cargo($id);
        $results['engine_details'] =$this->Admin_panel->select_h1_engine($id);
        $results['exterior_details'] =$this->Admin_panel->select_h1_exterior($id);
        $results['feul_details'] =$this->Admin_panel->select_h1_feul_comsuption($id);
        $results['weight_details'] =$this->Admin_panel->select_h1_weight($id);
        $results['wheels_details'] =$this->Admin_panel->select_h1_wheels($id);
        $results['transmission_details'] =$this->Admin_panel->select_h1_transmission($id);

        exit(json_encode($results));

    }
}

I want to get all tables data and show it on view through ajax, jason but i have not idea about that how to handle multidimensional array(php) encoded into jason ,

Hello here is ajax code

$(document).ready(function(){
        $('#selTrim').change(function(){
            var trim_id1 = $(this).val();
            if(trim_id1){
                $.ajax({
                    type:'POST',
                    url:'<?= base_url();?>H1_specifications/h1_trim_detail',
                    data:'trim_id1='+trim_id1,
                    datatype:'Json',
                    success:function(response){
                        if(response.success==1){
                            $('#PlaceWhereYouWantToPlace').html(response.data);
                        }else{
                            $('#').text('No Data Found');
                        }
                    }
                }); 
            }
        });
    });

Controller

I always prepare my HTML code in the controller and simply place in view. The reason of preparing ajax HTML data in the controller is I can easily change and manipulate the data and HTML content.

foreach($propertyAddedData as $propertyAddedData){
$table_data .= '<tr>
                <td><a href='.base_url('property/property_details/'.base64_encode($propertyAddedData['data']['id'])).'>'.$propertyAddedData['data']['property_address'].'</a></td>
                <td>$'.$propertyAddedData['data']['price'].'</td>
                <td>'.$propertyAddedData['data']['bedrooms'].'</td>
                <td>'.$propertyAddedData['data']['bathrooms'].'</td>
            </tr>';
        $error = 0; 
}else{
    $error = 1; 
}

$propertyData = '<div class="mt-property-desc">
                 <table class="table-fixed">
                    <thead>
                        <tr>
                            <th>Address</th>
                            <th>Price</th>
                            <th>Beds</th>
                            <th>Baths</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td colspan="3"><span class="gap-50"></span></td>
                        </tr>
                        '.$table_data.'
                    </tbody>
                    </table>
            </div>';
$data    = $propertyData;
echo json_encode(array('error'=>$error,'data'=>$data));
exit;           

I hope it will make you clear but if didn't please let me know i will explain you

Passing Multidimensional Arrays from PHP to JavaScript using JSON, parse to handle the results of PHP's json_encode , in which case you would use the following instead, as one of the examples below demonstrates: var ar = JSON​. I'm trying to get my head around grabbing all variable I need in Codeigniter and then passing those values to the view. I understand the concept, but am getting stuck on iterating through an array

try:

success: function(data) {
     console.log( data.overview_detail);
     console.log( data.cargo_details);
     ... and so on
}

JSON to PHP Using json_decode, JSON String to Multidimensional Array. The json_decode function can also be used to convert more complex data structures held in JSON strings. The JSON string  Array in Foreach (CodeIgniter) php,arrays I'm just a beginner in PHP coding. I've been reading through a tutorial, but having some trouble with basic PHP concepts

// use json_encode() from php end and To decode in javascript use below code

var obj = JSON.parse(data);

how to pass array from view to controller, PHP Code: <form method="post" action="collect_vals.php"> a form to a controller. Using Ajax I need to pass an array of data to the controller  Passing Multidimensional Arrays from PHP to JavaScript. Previously we demonstrated how the json_encode function can be used to pass PHP arrays to JSON strings and JavaScript arrays. On this page we continue the demonstration with multidimensional arrays. One array is numerically indexed, two others are mixed numerical and associative.

Here The controller function

public function h1_trim_detail(){
        $id = $_POST['trim_id1'];
        if(isset($_POST['trim_id1']) !== NULL){
            $this->load->model('Admin_panel');

            $rows7 =$this->Admin_panel->select_h1_details_specification($id);
            $rows = $this->Admin_panel->select_h1_Cargo($id);
            $rows1 =$this->Admin_panel->select_h1_engine($id);
            $rows2 =$this->Admin_panel->select_h1_exterior($id);
            $rows3 =$this->Admin_panel->select_h1_feul_comsuption($id);
            $rows4 =$this->Admin_panel->select_h1_weight($id);
            $rows5 =$this->Admin_panel->select_h1_wheels($id);
            $rows6 =$this->Admin_panel->select_h1_transmission($id);

            echo "<div id='specContWrap'>";
                if($rows7 !== false){
                    echo "<div id='overViewWrap' class='overViewTableCon'>";
                    echo "<div class='overViewTable on'>";
                        echo "<div class='tit'>Overview</div>";
                        echo "<table class='infoTableType'>";
                            echo "<caption>Overview Table</caption>";
                            echo "<tbody>";
                                    echo "<tr>";
                                        echo "<th scope='row'>Category</th>";
                                        echo "<td>Commercial</td>";
                                        echo "</tr>";
                                foreach($rows7 as $row){
                                    echo "<tr>";
                                    echo "<th scope='row'>Product label</th>";
                                    echo "<td>".$row['name']."</td>";
                                    echo "</tr>";
                                echo "<tr>";
                                    echo "<th scope='row'>Model name</th>";
                                    echo "<td>". $row['name_car']."</td>";
                                    echo "</tr>";
                                echo "<tr>";
                                    echo "<th scope='row'>Seats</th>";
                                    echo "<td>". $row['seats']."</td>";
                                    echo "</tr>";
                                }

                                echo "</tbody>";
                            echo "</table>";
                        echo "</div>";
                    echo "</div>";

                }else{
                    echo "Not Found";
                }


                echo "<div class='togListWrap small  sepcListWrap' id='overviewh1'>";
                    echo "<div class='togList'>";
                        echo "<button class='btnOpenClose'>Performance</button>";
                        echo "<div class='togCont' id='performanceWrap'>";

                        echo "<div class='performance on'>";
                            if($rows1 !== false){
                                echo "<dl class='specList'>";
                                echo "<dt>Engine Specification</dt>";
                                foreach($rows1 as $row){
                                echo "<dd>";
                                    echo "<dl>";
                                        echo "<dt>".$row['label']."</dt>";
                                        echo "<dd>".$row['descriptions']."</dd>";
                                    echo "</dl>";
                                echo "</dd>";
                                }
                                echo "</dl><br>";
                            }else{
                                echo "<dt>Engine Specification Not Found</dt><br>";
                            }
                            if($rows3 !== false){
                                echo "<dl class='specList'>";
                                echo "<dt>Feul Comsuption Specification</dt>";
                                foreach($rows3 as $row){
                                echo "<dd>";
                                    echo "<dl>";
                                        echo "<dt>".$row['label']."</dt>";
                                        echo "<dd>".$row['description']."</dd>";
                                    echo "</dl>";
                                echo "</dd>";
                                }

                                echo "</dl><br>";
                            }else{
                                echo "<dt>Feul Comsuption Specification Not Found</dt><br>";
                            }
                            if($rows6 !== false){
                                echo "<dl class='specList'>";
                                echo "<dt>Transmission Specification</dt>";
                                foreach($rows6 as $row){
                                echo "<dd>";
                                    echo "<dl>";
                                        echo "<dt>".$row['label']."</dt>";
                                        echo "<dd>".$row['descriptions']."</dd>";
                                    echo "</dl>";
                                echo "</dd>";
                                }

                                echo "</dl><br>";
                            }else{
                                echo "<dt>Transmission Specification Not Found</dt><br>";
                            }
                        echo "</div>";
                    echo "</div>";
                    echo "</div>";

                    echo "<div class='togList'>";
                        echo "<button class='btnOpenClose'>Dimension</button>";
                        echo "<div class='togCont' id='DimensionWrap'>";

                        echo "<div class='Dimension on'>";
                            if($rows2 !== false){
                                echo "<dl class='specList'>";
                                echo "<dt>Exterior Specification</dt>";
                                foreach($rows2 as $row){
                                echo "<dd>";
                                    echo "<dl>";
                                        echo "<dt>".$row['label']."</dt>";
                                        echo "<dd>".$row['description']."</dd>";
                                    echo "</dl>";
                                echo "</dd>";
                                }
                                echo "</dl><br>";
                            }else{
                                echo "<dt>Exterior Specification Not Found</dt><br>";
                            }
                            if($rows4 !== false){
                                echo "<dl class='specList'>";
                                echo "<dt>Weight Specification</dt>";
                                foreach($rows4 as $row){
                                echo "<dd>";
                                    echo "<dl>";
                                        echo "<dt>".$row['label']."</dt>";
                                        echo "<dd>".$row['description']."</dd>";
                                    echo "</dl>";
                                echo "</dd><br>";
                                }

                                echo "</dl><br>";
                            }else{
                                echo "<dt>Weight Specification Not Found</dt><br>";
                            }

                            if($rows !== false){
                                echo "<dl class='specList'>";
                                echo "<dt>Cargo Specification</dt>";
                                foreach($rows as $row){
                                echo "<dd>";
                                    echo "<dl>";
                                        echo "<dt>".$row['label']."</dt>";
                                        echo "<dd>".$row['description']."</dd>";
                                    echo "</dl>";
                                echo "</dd>";
                                }
                                echo "</dl><br>";
                            }else{
                                echo "<dt>Cargo Specification Not Found</dt><br>";
                            }
                        echo "</div>";
                    echo "</div>";
                    echo "</div>";

                    echo "<div class='togList'>";
                        echo "<button class='btnOpenClose'>Wheels</button>";
                        echo "<div class='togCont' id='WheelsWrap'>";

                        echo "<div class='Wheels on'>";
                            if($rows5 !== false){
                                echo "<dl class='specList'>";
                                echo "<dt>Wheels Specification</dt>";
                                foreach($rows5 as $row){
                                echo "<dd>";
                                    echo "<dl>";
                                        echo "<dt>".$row['label']."</dt>";
                                        echo "<dd>".$row['description']."</dd>";
                                    echo "</dl>";
                                echo "</dd>";
                                }
                                echo "</dl><br>";
                            }else{
                                echo "<dt>Wheels Specification Not Found</dt><br>";
                            }

                        echo "</div>";
                    echo "</div>";
                    echo "</div>";
                echo "</div>";
            echo "</div>";
        }
    }

Here is the j Query on change Function

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $('#selTrim').change(function(){
            var trim_id1 = $(this).val();
            if(trim_id1){
                $.ajax({
                    type:'POST',
                    url:'<?= base_url();?>H1_specifications/h1_trim_detail',
                    data:'trim_id1='+trim_id1,
                    success:function(data){
                        $('#specContWrap').html(data);
                    }
                }); 
            }
        });
    });
    </script>

Here is the Html where i want to display data from database using ajax

<div class="specsContent" id="spec" data-modelyear="T9-2015">
            <!-- specsContent : s -->
            <div class="subTit2">Specification</div>
            <div class="sortSelect">
                <div class="tit">Choose a Trim</div>
                <select title="Choose a Trim" id="selTrim" name="selTrim" class="bgLB">
                    <option disabled selected>Choose your Trim</option>
                    <?php
                    foreach($result as $row){
                        echo "<option value=".$row['trim_id'].">".$row['name']."</option>";
                    }
                ?>
                </select>
            </div>
            <div id="specContWrap">
                <div id="overViewWrap" class="overViewTableCon">
                    <div class="overViewTable on">
                        <div class="tit">Overview</div>
                        <table class="infoTableType">
                            <caption>Overview Table</caption>
                            <tbody>
                                <tr>
                                    <th scope="row">Category</th>
                                    <td> Commercial</td>
                                </tr>
                                <tr>
                                    <th scope="row">Product label</th>
                                    <td> Select Trim </td>
                                </tr>
                                <tr>
                                    <th scope="row">Model name</th>
                                    <td> H-1</td>
                                </tr>
                                <tr>
                                    <th scope="row">Seats</th>
                                    <td>Select Trim</td>
                                </tr>
                            </tbody>
                        </table>
                    </div><!-- //.overViewTable -->
                </div>
                <!-- toggle : s -->
                <div class="togListWrap small  sepcListWrap" id="overviewh1">
                    <div class="togList">
                        <button class="btnOpenClose ">Performance</button>
                        <div class="togCont " id="performanceWrap">
                        Select Trim
                        </div>
                    </div>
                    <div class="togList">
                        <button class="btnOpenClose ">Dimension</button>
                        <div class="togCont " id="dimensionWrap">
                            <div class="dimension on">
                            Select Trim
                            </div><!-- //demention -->
                        </div>
                    </div>
                    <div class="togList">
                        <button class="btnOpenClose ">Wheels</button>
                        <div class="togCont " id="wheelsWrap">
                            <div class="wheels on">
                            Select Trim
                            </div><!-- //wheels -->
                        </div>
                    </div>
                </div>
            </div>
            <div id="htmSpecContWrap" class="hide"></div>

How to Encode and Decode JSON Data in PHP, PHP MySQL CRUD Application PHP MySQL Ajax Search PHP MySQL Login JSON data structures are very similar to PHP arrays. The value being encoded can be any PHP data type except a resource, like a database or file handle. an PHP indexed array as JSON object by using the JSON_FORCE_OBJECT  Pass PHP Arrays to JSON and JS with json_encode. The PHP json_encode function translates the data passed to it to a JSON string which can then be output to a JavaScript variable. We demonstrate on this page with single level arrays. Other pages demonstrate using json_encode with multi-dimensional arrays and scalar values.

Store HTML Table Values in a Javascript Array and Send to a PHP , The jQuery .each function is used to iterate through the table rows storing the Store HTML Table Values into Multidimensional Javascript Array Object For this AJAX method, the JSON encoded TableData array is being  PHP - Multidimensional Arrays. A multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.

CodeIgniter Convert Query to Json & Insert Into Database, In this tutorial, we will learn to convert CodeIgniter query to json and also to insert jQuery Ajax; jQuery Ajax Post Data · Form Submission Using jQuery; Sending Emails Using json function we can convert array values into json string and store Also, open application/config/autoload.php and load database library and​  Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.

json_encode - Manual, The encoding is affected by the supplied options and additionally the encoding of float echo "Associative array always output as object: ", json_encode($d, To summarise, always test that your JSON decoder can handle the output you  Models : json_model.php. Json_model interact with database and stored json string into database. Note: if you want decode json data, just retrieve data from database and use json_decode(); to convert json string into array.

Comments
  • Thank you Buddy Yes I have done this method, But their is the problem with this , That code is not working on those Browsers which Have VPN install in them
  • request not responding even just for echo "Done";
  • Can you post your full controlller and ajax and html code
  • it's up their i am posting previous code without jason
  • Please Check Below Code
  • Thanks For reply Buddy but its is Showing "Undefined" in console
  • @FahadNadeem echo json_encode($result); instead of exit;
  • Thank You Bro then how to Show up in the Html here is the parsed array " {overview_details: Array(1)}overview_details: Array(1)0: cat_id: "4"description: "The "Dynamic Precision" advanced with Hyundai's Fluidic Sculpture 2.0 philosophy captures the modernity of the sporty, avant-garde design of the brand-new H-1."name: "BusA2e"name_car: "H-1"seats: "4"trim_id: "3"vehicle_id: "7"proto: Objectlength: 1__proto__: Array(0)__proto__: Object temp1"
  • from php end decode this json data using json_decode() function. you will get your data in array format again.