src/View/Helper/FormatHelper.php
public function array2table(array $array, array $options = [], array $attributes = []) {
		$defaults = [
			'null' => '&nbsp;',
			'recursive' => false,
			'heading' => true,
			'escape' => true
		];
		$options += $defaults;

		// Sanity check
		if (empty($array)) {
			return '';
		}

		if (!isset($array[0]) || !is_array($array[0])) {
			$array = [$array];
		}

		$attributes += [
			'class' => 'table'
		];

		$attributes = $this->template->formatAttributes($attributes);

		// Start the table
		$table = "<table$attributes>\n";

		if ($options['heading']) {
			// The header
			$table .= "\t<tr>";
			// Take the keys from the first row as the headings
			foreach (array_keys($array[0]) as $heading) {
				$table .= '<th>' . ($options['escape'] ? h($heading) : $heading) . '</th>';
			}
			$table .= "</tr>\n";
		}

		// The body
		foreach ($array as $row) {
			$table .= "\t<tr>";
			foreach ($row as $cell) {
				$table .= '<td>';

				// Cast objects
				if (is_object($cell)) {
					$cell = (array)$cell;
				}

				if ($options['recursive'] && is_array($cell) && !empty($cell)) {
					// Recursive mode
					$table .= "\n" . static::array2table($cell, $options) . "\n";
				} else {
					$table .= (!is_array($cell) && strlen($cell) > 0) ? ($options['escape'] ? h(
						$cell
					) : $cell) : $options['null'];
				}

				$table .= '</td>';
			}

			$table .= "</tr>\n";
		}

		$table .= '</table>';
		return $table;
	}
public function array2table(array $array, array $options = [], array $attributes = []) {
		$defaults = [
			'null' => '&nbsp;',
			'recursive' => false,
			'heading' => true,
			'escape' => true
		];
		$options += $defaults;

		// Sanity check
		if (empty($array)) {
			return '';
		}

		if (!isset($array[0]) || !is_array($array[0])) {
			$array = [$array];
		}

		$attributes += [
			'class' => 'table'
		];

		$attributes = $this->template->formatAttributes($attributes);

		// Start the table
		$table = "<table$attributes>\n";

		if ($options['heading']) {
			// The header
			$table .= "\t<tr>";
			// Take the keys from the first row as the headings
			foreach (array_keys($array[0]) as $heading) {
				$table .= '<th>' . ($options['escape'] ? h($heading) : $heading) . '</th>';
			}
			$table .= "</tr>\n";
		}

		// The body
		foreach ($array as $row) {
			$table .= "\t<tr>";
			foreach ($row as $cell) {
				$table .= '<td>';

				// Cast objects
				if (is_object($cell)) {
					$cell = (array)$cell;
				}

				if ($options['recursive'] && is_array($cell) && !empty($cell)) {
					// Recursive mode
					$table .= "\n" . static::array2table($cell, $options) . "\n";
				} else {
					$table .= (!is_array($cell) && strlen($cell) > 0) ? ($options['escape'] ? h(
						$cell
					) : $cell) : $options['null'];
				}

				$table .= '</td>';
			}

			$table .= "</tr>\n";
		}

		$table .= '</table>';
		return $table;
	}
public function array2table ( array $ array , array $ options = [ ] , array $ attributes = [ ] ) { $ defaults = [ 'null' => '&nbsp;' , 'recursive' => false , 'heading' => true , 'escape' => true ] ; $ options += $ defaults ; // Sanity check if ( empty ( $ array ) ) { return '' ; } if ( ! isset ( $ array [ 0 ] ) || ! is_array ( $ array [ 0 ] ) ) { $ array = [ $ array ] ; } $ attributes += [ 'class' => 'table' ] ; $ attributes = $ this -> template -> formatAttributes ( $ attributes ) ; // Start the table $ table = "<table$attributes>\n" ; if ( $ options [ 'heading' ] ) { // The header $ table .= "\t<tr>" ; // Take the keys from the first row as the headings foreach ( array_keys ( $ array [ 0 ] ) as $ heading ) { $ table .= '<th>' . ( $ options [ 'escape' ] ? h ( $ heading ) : $ heading ) . '</th>' ; } $ table .= "</tr>\n" ; } // The body foreach ( $ array as $ row ) { $ table .= "\t<tr>" ; foreach ( $ row as $ cell ) { $ table .= '<td>' ; // Cast objects if ( is_object ( $ cell ) ) { $ cell = ( array ) $ cell ; } if ( $ options [ 'recursive' ] && is_array ( $ cell ) && ! empty ( $ cell ) ) { // Recursive mode $ table .= "\n" . static :: array2table ( $ cell , $ options ) . "\n" ; } else { $ table .= ( ! is_array ( $ cell ) && strlen ( $ cell ) > 0 ) ? ( $ options [ 'escape' ] ? h ( $ cell ) : $ cell ) : $ options [ 'null' ] ; } $ table .= '</td>' ; } $ table .= "</tr>\n" ; } $ table .= '</table>' ; return $ table ; }
Translate a result array into a HTML table

@todo Move to Text Helper etc.

Options:
- recursive: Recursively generate tables for multi-dimensional arrays
- heading: Display the first as heading row (th)
- escape: Defaults to true
- null: Null value

@author Aidan Lister <aidan@php.net>
@version 1.3.2
@link http://aidanlister.com/2004/04/converting-arrays-to-human-readable-tables/
@param array $array The result (numericaly keyed, associative inner) array.
@param array $options
@param array $attributes For the table
@return string
Translate a result array into a HTML table