Search code examples
phploopsforeachnested-classbulletedlist

Beautify output of nested classes


I am currently using a rather extensive API that changes on a regular basis. As I am constantly pulling the latest API specifications, I decided to dump all of the data to a page for easier viewing. As it stands now, I am just using var_dump but due to the length it can be pretty rough on the eyes.

As the data is essentially just nested classes, I would like to do the first level as roman numerals, second level as bullet points, third as alphabet, etc. Is there any easy simplistic approach to do this without just nesting dozens of foreach loops just to account for as many levels possible?

Example data:

object(stdClass)#58 (5) {
  ["MinecraftModule"]=>
  object(stdClass)#59 (24) {
    ["BukGetCategories"]=>
    object(stdClass)#60 (3) {
      ["Parameters"]=>
      array(0) {
      }
      ["ReturnTypeName"]=>
      string(7) "JObject"
      ["IsComplexType"]=>
      bool(true)
    }
    ["BukGetPopularPlugins"]=>
    object(stdClass)#61 (3) {
      ["Parameters"]=>
      array(0) {
      }
      ["ReturnTypeName"]=>
      string(7) "JObject"
      ["IsComplexType"]=>
      bool(true)
    }
    ["BukGetPluginsForCategory"]=>
    object(stdClass)#62 (3) {
      ["Parameters"]=>
      array(3) {
        [0]=>
        object(stdClass)#63 (2) {
          ["Name"]=>
          string(12) "CategoryName"
          ["TypeName"]=>
          string(6) "String"
        }
        [1]=>
        object(stdClass)#64 (2) {
          ["Name"]=>
          string(10) "StartIndex"
          ["TypeName"]=>
          string(5) "Int32"
        }
        [2]=>
        object(stdClass)#65 (2) {
          ["Name"]=>
          string(8) "PageSize"
          ["TypeName"]=>
          string(5) "Int32"
        }
      }
      ["ReturnTypeName"]=>
      string(7) "JObject"
      ["IsComplexType"]=>
      bool(true)
    }

Solution

  • Use the following code to output an array or object with formatting:

    echo "<pre>";
    print_r($var);
    echo "</pre>";