Function Decoding

Not so long ago, this function was sent in response to a question. As a student, I am interested in understanding this function. However, I cannot make it work as it is. The poster did not say that it checked the function, so it could be a โ€œconceptualโ€ message designed to indicate direction. OTOH, I cannot refer to it correctly. Please help as I spent some time trying to figure this out.

(the $ data array is the one I just used to run the function.)

<?php $data = array("a" => "January", "b" => "February", "c" => "March" ); render_table ( $data ) ; echo $html ; //=========================================== function render_table($data) { $html = '<table>'; $tr = array(); foreach (array_keys($data[0]) as $key) { $tr[] = '<th>' . htmlspecialchars($key) . '</th>'; } $html .= "\n" . '<thead>' . "\n" . '<tr>' . "\n" . implode("\n", $tr) . '</tr>' . '</thead>'; $tbody = array(); foreach ($data as $row) { $tr = array(); foreach ($row as $value) { $tr[] = '<td>' . htmlspecialchars($value) . '</td>'; } $tbody[] = '<tr>' . "\n" . implode("\n", $tr) . '</tr>'; } $html .= "\n" . '<tbody>' . "\n" . implode("\n", $tbody) . '</tbody>'; $html .= '</table>'; return $html; } ?> 
+4
source share
3 answers

Firstly, your data is erroneous. The function takes an array of arrays as an argument.

Pay attention to the line

  foreach (array_keys($data[0]) as $key) { 

... this function call is also incorrect; it must NOT accept the first element, it must accept the entire array, since it uses keys from the array.

Try it: first change the call to array_keys to use the whole array, i.e.

 foreach(array_keys($data AS $key) 

then change your input and function call:

 $data= array( 'data1' => array('one','two','three'), 'data2' => array('four','five','six'), 'data3' => array('seven','eight','nine') ); echo render_table($data); 
+5
source

Try as follows:

 <?php $data = array("a" => "January", "b" => "February", "c" => "March" ); $html = render_table ( $data ) ; echo $html ; //=========================================== function render_table($data) { $html = '<table>'; $tr = array(); foreach (array_keys($data[0]) as $key) { $tr[] = '<th>' . htmlspecialchars($key) . '</th>'; } $html .= "\n" . '<thead>' . "\n" . '<tr>' . "\n" . implode("\n", $tr) . '</tr>' . '</thead>'; $tbody = array(); foreach ($data as $row) { $tr = array(); foreach ($row as $value) { $tr[] = '<td>' . htmlspecialchars($value) . '</td>'; } $tbody[] = '<tr>' . "\n" . implode("\n", $tr) . '</tr>'; } $html .= "\n" . '<tbody>' . "\n" . implode("\n", $tbody) . '</tbody>'; $html .= '</table>'; return $html; } ?> 

Basically, a table representation of the $ data is created.

+2
source

The function creates a table to view the data transferred to it. In your code you have

 ... render_table ( $data ) ; echo $html ; 

However, $html is empty in this case. A variable created in the render_table function is not passed outside the function unless you have assigned the return value of the function to a variable, for example:

 ... $html = render_table ( $data ) ; echo $html ; 

Then it echoes the HTML table.

+1
source

Source: https://habr.com/ru/post/1299262/


All Articles