PHP display array content

PHP display array content



How to display PHP array content is something that every developer asks sooner or later when playing and working with arrays in PHP. Displaying content of array in PHP is not so complicated for simple arrays, let's say for a single-dimensional array, but it can get nasty for multi-dimensional arrays. So, how to display content of a multi-dimensional PHP array?

PHP display array contents - assumptions

Let's describe what we are working with. We have an array called $displayarraycontent. This array contains another array under the first key which is called for example $cars. The array corresponding to this key contains information about Toyota, Honda, and Saturn in the form of another array. Each array related to a particular car make or model includes more detailed information, and lastly the color of our Saturn car has two parameters. The following example demonstrates how our exemple array was created:

$displayarraycontent = array (
    "cars"  => array(
  array("make" => "Toyota", "model" => "Corolla", "color" => "red"),
  array("make" => "Honda", "model" => "Civic", "color" => "blue"),
  array("make" => "Saturn", "model" => "Sky",
   "car_color" => array("color" => "silver", "paint type" => "metalic"),
   ),
  ),
    "beer" => array(
  "good beer" => array("brand" => "Pilsner Urquel", "made in" => "Czech", "price" => "$2.60"),
  "also good beer" => array("make" => "Warsteiner", "made in" => "German", "price" => "2.10"),
  "good beer as well" => array("make" => "Baltika", "made in" => "Russia", "price" => "$2.30")
  ),
    "airplanes"   => array(
  "Boeing" => array("class" => "commercial", "flight range" => "4000 miles"),
  "Cesna" => array("class" => "executive jet", "flight range" => "1000 miles")
  )
);

The code above shows how our array was created. We can see that the original $displayarraycontent array branches out further down into more detailed levels. What you are looking at is a 4-dimensional array.

How to display array content

Now, let's take a look at our array from the other end. You are working with some array that may or may not look like our array. You have no idea what the guts of your array look like, but you know that you are looking for some value somewhere inside there, so you ask "How to display PHP array contents?" One way to display array contents is to use the print_r() PHP function, but you would not get a very readable output for complicated arrays with this function. There is a better alternative.

The following code loops through all the keys and values of given array and displays their content.

function displayArrayContentFunction($arrayname,$tab="&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp",$indent=0) {
 $curtab ="";
 $returnvalues = "";
 while(list($key, $value) = each($arrayname)) {
  for($i=0; $i<$indent; $i++) {
   $curtab .= $tab;
   }
  if (is_array($value)) {
   $returnvalues .= "$curtab$key : Array: <br />$curtab{<br />\n";
   $returnvalues .= displayArrayContentFunction($value,$tab,$indent+1)."$curtab}<br />\n";
   }
  else $returnvalues .= "$curtab$key => $value<br />\n";
  $curtab = NULL;
  }
 return $returnvalues;
}

You can see that this function goes through each key => value pair and when it encounters value in the form of another array, it calls itself until it reaches the end of the array. The function is used in a way that you just echo the function with the array as its argument.

echo displayArrayContentFunction($displayarraycontent);

Now let's take a look at what this look like in the real world.

How to display array content example

What happens if we put the function to work on our sample $displayarraycontent array? The following shows how the function displays the content of the array:

cars : Array:
{
      0 : Array:
      {
            make => Toyota
            model => Corolla
            color => red
      }
      1 : Array:
      {
            make => Honda
            model => Civic
            color => blue
      }
      2 : Array:
      {
            make => Saturn
            model => Sky
            car_color : Array:
            {
                  color => silver
                  paint type => metalitic
            }
      }
}
beer : Array:
{
      good beer : Array:
      {
            brand => Pilsner Urquel
            made in => Czech
            price => $2.60
      }
      also good beer : Array:
      {
            make => Warsteiner
            made in => German
            price => 2.10
      }
      good beer as well : Array:
      {
            make => Baltika
            made in => Russia
            price => $2.30
      }
}
airplanes : Array:
{
      boeing : Array:
      {
            class => commercial
            flight range => 4000 miles
      }
      cesna : Array:
      {
            class => executive jet
            flight range => 1000 miles
      }
}

As you can see, this way of displaying array content is very easy and nicely comprehendible. Have fun.

Anything else you can recommend?

Yea, we have many other articles that might interest you, for example:

How to check if string contains substring in PHP
PHP loop through string
JavaScript document.write(): How to view the HTML?
Carriage return and line feed problems

You can also find some ideas or ask a question in our programming discussion forum.

.

Discuss this article or this topic in our discussion forum:
(The table bellow shows a list of 8 most recent topics posted in our discussion forum. Visit our discussion forum to see more. It is possible the links below are not related to this page, but you can be certain you will find related posts in the discussion forum. You can post one yourself too.)
Email this article to a friend:
TO: 
FROM: 
2 + 6 - 3 = 
.
How can I link to this web page?

It is easy, just include the code provided below into your HTML code.

<a href="http://www.maxi-pedia.com/php+display+array+content" title="www.Maxi-Pedia.com: PHP display array content" target="_blank">PHP display array content</a>
.