Search code examples
phparraysmultidimensional-array

How to access multidimensional data with loops


I wanted to show everything what is on the arrays but it doesn't seems to show all information, and each server might have more than 2 partition, below I written was just 2 partition, other server might have more than 2. Is it possible to detect how many partition inside the server and display all? below is my code:-

<?php
$rangehourly = "129600";
$rangedaily = "604800";

$graph_list = array(
 'myserver.com' => array(
 
  '/home' => array(
   'daily'=> 'https://longserverurlhere.com',
   'hourly'=> 'https://longserverurlhere.com'
  ),
  
  '/var/www/html/' => array(
   'daily'=> 'https://longserverurlhere',
   'hourly'=> 'https://longserverurlhere'
  )
 )
);
 
 foreach($graph_list as $servername => $value) {
    echo $servername . "<br />";
    foreach($value as $a => $part) {
        echo $a .'<br>'. $part[0].'<br>'.$part[1];
    
    foreach($part as $b => $range) {
    echo $b . '<br>'. $range[0]. '<br>'.$range[1];
    
    }
    }
    echo "<br> /><br />";
}
?>

I tried adding another foreach loop, it shows me invalid argument. Ignore the variables it will placed with url once it this issue is solved.


Solution

  • I suppose code should look like this:

    <?php
    $rangehourly = "129600";
    $rangedaily = "604800";
    
    $graph_list = array(
     'myserver.com' => array(
      '/home' => array(
       'daily'=> 'https://longserverurlhere.com',
       'hourly'=> 'https://longserverurlhere.com'
      ),
      '/var/www/html/' => array(
       'daily'=> 'https://longserverurlhere',
       'hourly'=> 'https://longserverurlhere'
      )
     )
    );
    
    foreach ($graph_list as $servername => $value) {
        echo htmlspecialchars($servername) . '<br>';
        foreach ($value as $folder => $ranges) {
            echo htmlspecialchars($folder) .'<br>';
            foreach ($ranges as $rangeName => $range)
                echo htmlspecialchars($rangeName) . '<br>' . htmlspecialchars($range) . '<br>';
        }
        echo '<br>';
    }
    ?>