Searching...
Wednesday 3 July 2013

Outputting Arrays


Now that you know what is an array? and how to create an array?, it is the time to learn how to output arrays. The most common way to output an array’s contents is by iterating over each key and echoing the corresponding value. For instance, a foreach statement does the trick nicely:

$states = array("Ohio", "Florida", "Texas");
foreach ($states AS $state) {
echo "{$state}<br />";
}

If you want to print an array of arrays or need to exercise a more exacting format standard over array output, consider using the vprint() function, which allows you to easily display array contents using the same formatting syntax used by the printf() and sprintf() functions introduced earlier. Here’s an example:

$customers = array();
$customers[] = array("Jason Gilmore", "jason@example.com", "614-999-9999");
$customers[] = array("Jesse James", "jesse@example.net", "818-999-9999");
$customers[] = array("Donald Duck", "donald@example.org", "212-999-9999");
foreach ($customers AS $customer) {
vprintf("<p>Name: %s<br />E-mail: %s<br />Phone: %s</p>", $customer);
}

Executing this code produces the following output:

<p>
Name: Jason Gilmore<br />
E-mail: jason@example.com
<br />Phone: 614-999-9999
</p>
<p>
Name: Jesse James<br />
E-mail: jesse@example.net<br />
Phone: 818-999-9999
</p>
<p>
Name: Donald Duck<br />
E-mail: donald@example.org<br />
Phone: 212-999-9999
</p>

If you’d like to send the formatted results to a string, you can use the vsprintf() function. It is the same as using sprintf() function.

Printing Arrays for Testing Purposes

The array contents in most of the previous examples have been displayed using comments. While this works great for instructional purposes, in the real world you’ll need to know how to easily output their contents to the screen for testing purposes. This is most commonly done with the print_r() function. Its prototype follows:

boolean print_r(mixed variable [, boolean return])

The print_r() function accepts a variable and sends its contents to standard output, returning TRUE on success and FALSE otherwise. This in itself isn’t particularly exciting, until you realize it will organize an array’s contents (as well as an object’s) into a readable format. For example, suppose you want to view the contents of an associative array consisting of states and their corresponding state capitals. You could call print_r() like this:

print_r($states);

This returns the following:

Array ( [Ohio] => Columbus [Iowa] => Des Moines [Arizona] => Phoenix )

The optional parameter return modifies the function’s behavior, causing it to return the output to the caller, rather than sending it to the standard output ( which is the browser window ). Therefore, if you want to return the contents of the preceding $states array, you just set return to TRUE:

$stateCapitals = print_r($states, TRUE);

Keep in mind the print_r() function isn’t the only way to output an array; it just offers a convenient means for doing so. You’re free to output arrays using a looping conditional, such as while or for; in fact, using these sorts of loops is required to implement many application features.

Note:- Always use quotes around a string literal array index ( except inside a double-quoted string, it's valid to not surround array indexes with quotes so "$foo[bar]" is valid). For example, $foo['bar'] is correct, while $foo[bar] is not. This does not mean to always quote the key. Do not quote keys which are constants or variables, as this will prevent PHP from interpreting them.

As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].

<?php
$array = array(
    "foo" => "bar",
    "bar" => "foo",
);
// as of PHP 5.4
$array = [
    "foo" => "bar",
    "bar" => "foo",
];
?>

0 comments:

Post a Comment

 
Back to top!