Furry
Furry

Reputation: 345

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.

Upvotes: 0

Views: 71

Answers (3)

KeGaWeb
KeGaWeb

Reputation: 25

Your keys are keywords, and you try to use numbers.

foreach($graph_list as $servername => $value) {
   echo $servername . "<br />";
   foreach($value as $a => $part) {
       echo $a .'<br>'. $part['daily'].'<br>'.$part['hourly'];
   }
   echo "<br> /><br />";
}

Upvotes: 0

Iłya Bursov
Iłya Bursov

Reputation: 24146

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>';
}
?>

Upvotes: 1

Craftein
Craftein

Reputation: 762

Try using

var_dump($array);

or

print_r($array);

If you choose the latter, I recommend adding this before that line:

echo "<pre>";

EDIT

HEHE. You edit it. I thought this is what you were looking for.

Upvotes: 0

Related Questions