CodeIgniter Forums
Multidimensional array read-out in view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Multidimensional array read-out in view (/showthread.php?tid=2546)



Multidimensional array read-out in view - El Forum - 08-11-2007

[eluser]Unknown[/eluser]
When I output print_r($data);

I get:

Code:
Array
(
    [view] => factuur_print_view
    [vars] => Array
        (
            [voornaam] => Sandy
            [achternaam] => Cohen
            [adres] => Somefancystreet 69
            [gemeente] => Orange County
            [tel] => 1000
            [mail] => [email protected]
            [stad] => California
            [postcode] => 17459
            [factuurnummer] => 1
            [datum] => 2007
            [betaald] => 1
            [0] => Array
                (
                    [id] => 1
                    [factuurnummer] => 1
                    [referentienummer] => ee
                    [omschrijving] => dfds
                    [aantal] => 2
                    [eenheidsprijs] => 10
                )

            [1] => Array
                (
                    [id] => 2
                    [factuurnummer] => 1
                    [referentienummer] => fgg
                    [omschrijving] => fdgf
                    [aantal] => 8
                    [eenheidsprijs] => 20
                )

        )

    [return] =>

My question is, how do I access [0] and [1] and put them in a loop?


Multidimensional array read-out in view - El Forum - 08-12-2007

[eluser]Michael Wales[/eluser]
CI automatically brings the var array to the top level - I've never dove into the code to see why. So, you should be able to loop through those arrays like so:

Code:
foreach ($0 as $key => $val) {
  echo $key . ": " . $val;
}

foreach ($1 as $key => $val) {
  echo $key . ": " . $val;
}

If that doesn't work for you just change $0 and $1 to $vars[0] and $vars[1], respectively.


Multidimensional array read-out in view - El Forum - 08-12-2007

[eluser]Unknown[/eluser]
I belive you want to have a multidimensional foreach loop that loops the numbers to, like:
Code:
foreach($var as $key => $val) {
   echo $key.'\n';
}
And this should return:
Code:
0
1
So what I figured out tonight is that you have to assign you multidimentional array with an own key when you pass the data to the view.

So you have to make sure you pass the vars to the view something like this:
Code:
[vars] => Array
        (
            [voornaam] => Sandy
            [achternaam] => Cohen
            [adres] => Somefancystreet 69
            [gemeente] => Orange County
            [tel] => 1000
            [mail] => [email protected]
            [stad] => California
            [postcode] => 17459
            [factuurnummer] => 1
            [datum] => 2007
            [betaald] => 1
            [loop] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [factuurnummer] => 1
                            [referentienummer] => ee
                            [omschrijving] => dfds
                            [aantal] => 2
                            [eenheidsprijs] => 10
                        )
                    [1] => Array
                        (
                            [id] => 2
                            [factuurnummer] => 1
                            [referentienummer] => fgg
                            [omschrijving] => fdgf
                            [aantal] => 8
                            [eenheidsprijs] => 20
                        )
                )
        )