Welcome Guest, Not a member yet? Register   Sign In
looping through entire multidimensional array passed to view
#1

[eluser]jordantkj[/eluser]
Hi,
What I'm trying to do is use a foreach to loop through all arrays and their values. Is there some way to simply loop through all of them without specifically using the array name (fpc1 and pic1)?

Code:
foreach($??? as $att1) {
   echo '<td>' . $att1 . '</td>';
}

Please see the array below:

Array
(
[fpc1] => Array
(
[0] => FPC
[1] => 0
[2] => 710-013052
[3] => 3
[4] => JJ8912
[5] => T320-FPC3-E
[6] => E-FPC Type 3
)

[pic1] => Array
(
[0] => PIC
[1] => 0
[2] => 750-009567
etc...

Thanks,
-T
#2

[eluser]cdonate[/eluser]
You have to do two foreach, one inside the other.

The first one is for the [fpc1] and [pic1], and the second one is for the content inside them.

It will first display the content of [fpc1] and then the content of [pic1].

Have you tried this solution?
#3

[eluser]Kamarg[/eluser]
Code:
function recurse($item, $level = 1)
{
  foreach($item as $k => $v)
  {
    if(is_array($v) || is_object($v))
      recurse($v, $level + 1);
    else
      echo "<td>$v</td>";
  }
}

recurse($your_data_array);

That should do the trick.

Edit: Sorry. Took me a min to realize you probably want to do more than output them all as one row. My brain has already switched off for the day it seems.
#4

[eluser]CroNiX[/eluser]
Code:
//traverses each outside array
foreach($array as $array_name => $sub_array)
{
  echo 'Working on array: <strong>' . $array_name . '</strong><br />';

  //traverses each element in the inner array
  foreach($sub_array as $item)
  {
    echo $item . '<br />';
  }
}
#5

[eluser]jordantkj[/eluser]
Thanks for the responses. The problem is that I'm passing the whole array to view and for whatever reason, I cannot access the data the same as when I'm in a controller or model. The documentation states this. When the array is passed to view, the internal array names ie. 'fpc1' become variables and the top level array 'router_array' cannot be referenced. So the solution would involve looping through all the array data while in view.

I can access individual sub-arrays using the following code, but if I have multiple sub-arrays with varying names, its impractical. I'm hoping someone might have run into this problem and figured out a simple trick to get it to work.

Code:
foreach($fpc1 as $att1) {
  echo '<td>' . $att1 . '</td>';
}

-T
#6

[eluser]CroNiX[/eluser]
It just depends on how you are sending the data to the view. I'm guessing you are sending the multidimensional array directly to your view instead of putting it in a variable first, which is why its separating your arrays.

Try this in the controller:
Code:
//Note: the keys in the $data array will become the variable names available to your view.
$data['custom'] = $your_multidimensional_array_from_the_first_post;
$this->load->view('view_file', $data);

Then in your view, you access your multidimensional array using $custom. If you used my code above and this code, you would just substitute $custom for $array in the loops.

#7

[eluser]jordantkj[/eluser]
That worked! I just needed that 'custom' so it could make a variable name for the topmost level.

Thanks alot!:cheese:
#8

[eluser]jwright[/eluser]
Here's a little function I've used to print a multi-dimensional array
Maybe you could modify it for your needs...
Code:
function printArray($array, $spaces = "")
{
   $retValue = "";

   if(is_array($array))
   {
      $spaces = $spaces
         ."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

      $retValue = $retValue."<br/>";

      foreach(array_keys($array) as $key)
      {
         $retValue = $retValue.$spaces
            ."<strong>".$key."</strong>"
            .printArray($array[$key],
               $spaces);
      }  
      $spaces = substr($spaces, 0, -30);
   }
   else $retValue =
      $retValue." - ".$array."<br/>";

   return $retValue;
}




Theme © iAndrew 2016 - Forum software by © MyBB