writeArrayNicely()

Well I just wrote this function and then realised it’s basically redundant. When you write out the contents of an array in PHP using var_dump() it writes it all out on one line. Wouldn’t it be great if it wrote it all out neatly, properly nested and everything? So I wrote a function that does that. Then I realised that if you view the source then var_dump() already does that. Damnit! I even made it handle arrays recursively!

	function writeArrayNicely($array, $depth=1){
		echo 'array(' . sizeof($array) . ') {<br>';
		foreach($array as $key=>$value){
			for ($x=0;$x<$depth;$x++)
				echo '&nbsp;&nbsp;&nbsp;&nbsp;';
			if (gettype($key) == 'string')
				echo '["' . $key . '"] => ';
			else
				echo '[' . $key . '] => ';
			if (gettype($value) == 'array')
				writeArrayNicely($value, ($depth+1));
			else
				echo gettype($value) . ' (' .  sizeof($value) . ') "' . $value . '"';
			echo '<br>';
		}
		for ($x=1;$x<$depth;$x++)
			echo '&nbsp;&nbsp;&nbsp;&nbsp;';
		echo '}';
	}
	
	$test = array(
		"string"=>"this is a string", 
		"integer"=>7, 
		"float"=>4.9, 
		6 => 42,
		"array"=>array(
			"string1" => "blah1",
			"string2" => "blah2"
		)
	);

writeArrayNicely($test);
Advertisements

About Mr Chimp

I make music, draw pictures, browse the internet, programme, and make sweet, sweet cups of tea until the early hours.
This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

6 Responses to writeArrayNicely()

  1. Miyanto says:

    nice working,

    you can also try this easily:

    <?php

    function echo_r($test){
    echo "

    ";
    print_r($test);
    echo "

    “;
    }

    $test = array(
    “string”=>”this is a string”,
    “integer”=>7,
    “float”=>4.9,
    6 => 42,
    “array”=>array(
    “string1” => “blah1”,
    “string2” => “blah2”
    )
    );
    echo_r($test);

    ?>

  2. Mr Chimp says:

    Hi Miyanto!

    That’s a good point, a whole lot less code, too!

    By the way, you can write sourcecode by using this syntax on wordpress:

    (sourcecode lang=’php’)

    code goes here!

    (/sourcecode)

    Just replace the brackets for square brackets. Then it’ll look like this:

    <?php
    
    function echo_r($test){
        echo "\n\n";
        print_r($test);
        echo "\n\n";
    }
    
    $test = array(
        "string"=>"this is a string",
        "integer"=>7,
        "float"=>4.9,
        6 => 42,
        "array"=>array(
            "string1″ => "blah1″,
            "string2″ => "blah2″
        )
    );
    echo_r($test);
    ?>
    

    (spot the difference!)

    • Miyanto says:

      yeah… you didn’t provide WYSIWYG editor :-p.

      (sourcecode lang=’php’)
      <?php

      function echo_r($test){
      echo "

      ";
         print_r($test);
         echo "

      “;
      }

      $test = array(
      “string”=>”this is a string”,
      “integer”=>7,
      “float”=>4.9,
      6 => 42,
      “array”=>array(
      “string1″ => “blah1″,
      “string2″ => “blah2″
      )
      );
      echo_r($test);

      ?>
      (/sourcecode)

  3. Skilldrick says:

    When I’m using var_dump, if I can’t be bothered to view source I enclose in

    :
    
    
    echo "<pre>" . var_dump($my_array) . "</pre>";
    
  4. Skilldrick says:

    That should have said I enclose in

    <pre>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s