Search code examples
phpcodeignitercodeigniter-2

How access the data from an associative array Codeigniter


I am new to PHP and codeigniter I have recently written a query in the model which is return as expected. The var_dump of the data array from the controller looks like

array(1){ ["industry"]=> array(1) { [0]=> array(3) { ["id"]=> string(1) "1" ["title"]=> string(11) "Recruitment" ["active"]=> string(1) "1" } } }

From the view I have tried using a foreach based on industries but am not getting anywhere when trying to access the arrays length and items. Your experience and guidance is much appreciated.


Solution

  • You are have 3 nested arrays, so you will need 3 nested foreach's. I tried to do a sample basing on your var_dump, see:

     $array = [
         "industry" => [
             "id" => "1",
             "title"=> "Recruitment",
             "active"=> "1"
          ],
     ];
    
    
     foreach ($array as $k) {
        foreach ($k as $k2) {
            foreach ($k2 as $k3=>$v) {
                echo $k3." --> ".$v."<br/>";
            }
        }
     }
    

    Let me know if it makes sense.