Array within array using foreach loop in codeigniter

Ahil Khan

I am new in codeigniter framework. I am making an array within array and within array using foreach loop for getting record from 3 tables. I have make like this way array

Array
(
[devices] => Array
    (
        [0] => Array
            (
                [id] => 1
                [device_name] => Smartphone
                [device_image] => device-images/1d57a675edbb2ff97c5bc35fce27517c.png
                [device_icon] => device-images/325af5a3587cdfc8f35ae345a2d432a3.png
                [all_manufacturers] => Array
                    (
                        [0] => Array
                            (
                                [id] => 1
                                [fk_device_id] => 1
                                [manufacture] => Apple
                                [models] => Array
                                    (
                                        [0] => Array
                                            (
                                                [id] => 1
                                                [fk_deviceid] => 1
                                                [fk_manufactureid] => 1
                                                [model_name] =>  iPhone 11 Pro Max 
                                            )

                                        [1] => Array
                                            (
                                                [id] => 2
                                                [fk_deviceid] => 1
                                                [fk_manufactureid] => 1
                                                [model_name] =>  iPhone 11 Pro 
                                            )

                                        [2] => Array
                                            (
                                                [id] => 3
                                                [fk_deviceid] => 1
                                                [fk_manufactureid] => 1
                                                [model_name] => iPhone 11
                                            )

                                    )

                            )

                        [1] => Array
                            (
                                [id] => 2
                                [fk_device_id] => 1
                                [manufacture] => Samsung
                                [models] => Array
                                    (
                                    )

                            )

                        [2] => Array
                            (
                                [id] => 3
                                [fk_device_id] => 1
                                [manufacture] => huawei
                                [models] => Array
                                    (
                                    )

                            )

                        [3] => Array
                            (
                                [id] => 5
                                [fk_device_id] => 1
                                [manufacture] => Sony
                            )

                    )

            )

        [1] => Array
            (
                [id] => 2
                [device_name] => Tablet
                [device_image] => device-images/fd9e3f3192547a89c07e30fbcb7e202b.png
                [device_icon] => device-images/9f39fb4583174c71e3b7f92ec3e5ed86.png
                [all_manufacturers] => Array
                    (
                        [0] => Array
                            (
                                [id] => 6
                                [fk_device_id] => 2
                                [manufacture] => Apple
                                [models] => Array
                                    (
                                        [0] => Array
                                            (
                                                [id] => 4
                                                [fk_deviceid] => 2
                                                [fk_manufactureid] => 6
                                                [model_name] => iPad 3 (2012)
                                            )

                                        [1] => Array
                                            (
                                                [id] => 6
                                                [fk_deviceid] => 2
                                                [fk_manufactureid] => 6
                                                [model_name] => iPad 2 (2011)
                                            )

                                        [2] => Array
                                            (
                                                [id] => 7
                                                [fk_deviceid] => 2
                                                [fk_manufactureid] => 6
                                                [model_name] => iPad 4 (2012)
                                            )

                                    )

                            )

                        [1] => Array
                            (
                                [id] => 7
                                [fk_device_id] => 2
                                [manufacture] => Microsoft
                            )

                    )

            )

        [2] => Array
            (
                [id] => 3
                [device_name] => Laptop
                [device_image] => device-images/60042e62d2fa440d3c467114538a1293.png
                [device_icon] => device-images/2044d96ba9274b5b1e3155103fa90506.png
                [all_manufacturers] => Array
                    (
                        [0] => Array
                            (
                                [id] => 11
                                [fk_device_id] => 3
                                [manufacture] => Apple
                            )

                    )

            )

        [3] => Array
            (
                [id] => 4
                [device_name] => Watch
                [device_image] => device-images/8f10aad91a793372e50a3275db132e4e.png
                [device_icon] => device-images/788dc55b9b6e0f805d63a42fd888e1bb.png
                [all_manufacturers] => Array
                    (
                        [0] => Array
                            (
                                [id] => 9
                                [fk_device_id] => 4
                                [manufacture] => Apple
                            )

                    )

            )

        [4] => Array
            (
                [id] => 6
                [device_name] => Camera
                [device_image] => device-images/88929629884491fd080505b56f1b2b0d.png
                [device_icon] => device-images/88fa1d78fe127e757c1cfc90ec925a14.png
            )

    )
   )

Issue in view page because showing notice error of Undefined index: all_manufacturers view page code is below

  <div class="row">
    <div class="col-lg-3 col-md-6">
        <div class="sidebar">
       <?php if($devices) { 
            foreach($devices as $dkey => $device) { 
              $all_manufacturers = $device["all_manufacturers"];?>
                <div class="dropdown cust-dropdown">
                 <button class="btn-dropdown cust-btn-dropdown" type="button"><div class="box-shop-icons"><img src="<?= base_url('assets/uploads/'.$device["device_icon"])?>" ></div>
                 <?= $device['device_name']?>
                 </button>
                 <div class="dropdown-menu-custom">
                 <div class="radio-btns cust-height">
                  <!-- inner radio button -->
                    <?php if ($all_manufacturers) {
                       foreach ($all_manufacturers as $manufacture) { ?>
                         <div class="dropdown">
                          <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown"><?= $manufacture['manufacture']?>
                          <span class="caret"></span></button>
                          <ul class="dropdown-menu">
                          <li><a href="#">iPhone 11 pro max</a></li>
                          <li><a href="#">iPhone 11 pro</a></li>
                          <li><a href="#">iPhone 11</a></li>
                          <li><a href="#">iPhone Xs max</a></li>
                          <li><a href="#">iPhone Xs</a></li>
                          <li><a href="#">iPhone X</a></li>
                          <li><a href="#">iPhone 8plus</a></li>
                       </ul>
                    </div>
             <?php } } ?> 
          </div>
      </div>
   </div>
 <?php } } ?> 
</div>

These 3 tables are linked with eachother

Spudly

The last array doesn't have "all_manufacturers" set on it, so the error is that you are trying to set $all_manufacturers without checking if it is set or not.

Remove this line at the top of the loop for devices:

$all_manufacturers = $device["all_manufacturers"];

Then change this code:

<?php if ($all_manufacturers) {
    foreach ($all_manufacturers as $manufacture) { ?>

To this:

if (!empty($device["all_manufacturers"])) {
    foreach ($device["all_manufacturers"] as $manufacture) {

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Array_Search within foreach loop

Multidimensional array in PHP using foreach loop

How to get the array values using foreach loop

Remove an element from an array within a foreach loop *without* using $key = > $val

How to loop array of objects using forEach?

Add to an array from within a loop using Ruby

Adding row_array to result_array foreach loop with codeigniter?

SQL query in Foreach loop using array variable

Generating a PHP Multidimensional array within a foreach loop

Displaying images from array using a foreach loop

Put values into array using foreach loop

Need to get keys form my array for a foreach loop for where in codeigniter

Insert array values with foreach using codeigniter

Modifying array within forEach

Create new array within existing array with each loop in foreach

codeigniter foreach (array) in foreach

Foreach loop within an array in laravel

Using foreach-loop to construct objects in the array

Increment values of an array using a for loop and a foreach

Convert Array to json using foreach loop

result_array value are not showing inside foreach loop in codeigniter

How to assign array value into variable inside foreach loop in codeigniter

Custom format array using foreach loop - PHP

How to modify array value using foreach loop?

Loop through an array of objects using forEach

Unable to access data from array inside foreach loop - Codeigniter

Exploding an array values within a foreach loop in PHP

SwiftUI: Using ForEach to dynamically generate a List of array data within an array

How do I call an array within an IIFE using a forEach loop?