I'm newbie in development and I have trouble with this below logic.
This is my sample DB.
$data = array(3) {
$x_count = 2;
[1] => $name = 'A';
$y_count = 2;
array(2) {
[1] => $name = 'A.1';
$z_count = 2;
array(2) {
[1] => $name = 'A.1.a';
$val = 1;
[2] => $name = 'A.1.b';
$val = 1;
}
[2] => $name = 'A.2';
$z_count = 2;
array(2) {
[1] => $name = 'A.2.a';
$val = 2;
[2] => $name = 'A.2.b';
$val = 2;
}
}
[2] => $name = 'B';
$y_count = 2;
array(2) {
[1] => $name = 'B.1';
$z_count = 2;
array(2) {
[1] => $name = 'B.1.a';
$val = 3;
[2] => $name = 'B.1.b';
$val = 3;
}
[2] => $name = 'A.2';
$z_count = 2;
array(2) {
[1] => $name = 'B.2.a';
$val = 4;
[2] => $name = 'B.2.b';
$val = 4;
}
}
}
And I have to print like this
I have no idea of using foreach loop to print data into this table or caculating the number of "colspan" for each of x, y, z title.
I really hope to receive some advice. Thanks a lot!
EDIT: the x_count, y_count and z_count are variables get from DB and it's not only 2.
EDIT2: my expected result looks like this:
<table>
<tr>
<th>x_name</th>
<td colspan = "7">A</td>
<td colspan = "7">B</td>
<th rowspan = "3">SUM</th>
</tr>
<tr>
<th>y_name</th>
<td colspan = "3">A.1</td>
<td colspan = "3">A.2</td>
<td rowspan = "2">SUM</td>
<td colspan = "3">B.1</td>
<td colspan = "3">B.2</td>
<td rowspan = "2">SUM</td>
</tr>
<tr>
<th>z_name</th>
<td>A.1.a</td>
<td>A.1.b</td>
<td>SUM</td>
<td>A.2.a</td>
<td>A.2.b</td>
<td>SUM</td>
<td>B.1.a</td>
<td>B.1.b</td>
<td>SUM</td>
<td>B.2.a</td>
<td>B.2.b</td>
<td>SUM</td>
</tr>
<tr>
<th>value</th>
<td>1</td>
<td>1</td>
<td>2</td>
<td>2</td>
<td>2</td>
<td>4</td>
<td>6</td>
<td>3</td>
<td>3</td>
<td>6</td>
<td>4</td>
<td>4</td>
<td>8</td>
<td>14</td>
<td>20</td>
</tr>
</table>
You don't need to add y_count, x_count, you can use count() function on the object $data instead.
Here is HTML's structure I created, you may take a look
For data easier to process, you can consider to use my data structure below
$data = [
[
"name" => 'A', // x_name
"children" => [
[
"name" => 'A.1', // y_name
"children" => [
[
"name" => 'A.1.a', // z_name
"value" => 1
],
]
]
]
],
...
];
NOTE: All the code's logic below is used for your provided HTML, not for my jsFiddle's HTML
First step: Run a loop on every element of array $data for processing x_name data. Assign each row's html to variable for further use
/* Init html variable for $x_name, $y_name, $z_name, $value */
$x_html = '';
$y_html = '';
$z_html = '';
$value_html = '';
foreach ($data as $x_index => $x_data) {
$x_children = $x_data['children'];
$x_colspan = 1; // this variable will be sum of 1 'SUM' column and each $y_colspan
foreach ($x_children as $y_index => $y_data) {
$y_children = $x_data['children'];
$y_colspan = count($y_children) + 1; // all of y's children column + SUM column
// Update x_colspan
$x_colspan += $y_colspan;
// process assign html to $y_html
$y_html .= '<td colspan = "'.$y_colspan.'">'.$y_data['name'].'</td>';
// add SUM column
if ($y_index == count($x_children)-1) {
$y_html .= '<td>SUM</td>';
}
$sum = 0;
foreach ($y_children as $z_index => $z_data) {
$z_value = $z_data['value'];
$z_html .= '<td>'.$z_data['name'].'</td>';
// add SUM column for every looped 2 $z_data column
if ($z_index == count($y_children)-1) {
$z_html .= '<td>SUM</td>';
}
$sum += $z_value; // sum all value of current y_children
$value_html .= '<td>'.$z_data['value'].'</td>';
}
// After finish assign z_html, value_html, sum it all
$value_html .= '<td>'.$sum.'</td>'
}
$x_html .= '<td colspan = "'.$x_colspan.'">'.$x_data['name'].'</td>';
}
Last step: you print out all variable x_html, y_html, z_html, value_html. Merge it with the actual table html
$table_html = '
<table>
<tr>
<th>x_name</th>
'.$x_html.'
<th rowspan = "3">SUM</th>
</tr>
<tr>
<th>y_name</th>
'.$y_html.'
</tr>
<tr>
<th>z_name</th>
'.$z_html.'
</tr>
<tr>
<th>z_name</th>
'.$value_html.'
</tr>
</table>
';
This code above may be not runable, it's just a idea. Hope it would help you
Related
I have a table which displays data fetched from the database. I have done this in PHP say $data_array();
What I want is to find the record with same Email address and highlight those rows with Red. Meaning, if an email address is repeated in the table twice or thrice, all the 3 rows will get Red highlighted.
I have done this way:
$exist = array();
foreach($data_array as $da){
if(in_array($da['Email'], $exist)){
$row_color = "#f00"; // Red
}else{
$row_color = "#fff"; // White
}
echo "<tr style='color:".$row_color.";'>";
echo "<td>".$da['Email']."</td>";
echo "</tr>";
$exist[] = $da['Email'];
}
The above works and displays the second and the third row highlighted but not the first one. Obviously my code has nothing which will highlight the first record which has repeated value.
How can I do this? Can be done in PHP or Jquery also. Any help??
in jQuery, you can use filter() and use includes() to check if <td>'s text is in the array.
var emails = ['email1#bla.com', 'select#bla.com'];
$('table td').filter(function() {
return emails.includes($(this).text().trim());
}).css('backgroundColor', 'red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<th>Company</th>
</tr>
<tr>
<td>email1#bla.com</td>
</tr>
<tr>
<td>select#bla.com</td>
</tr>
<tr>
<td>doNotSelect#bla.com</td>
</tr>
<tr>
<td>email1#bla.com</td>
</tr>
<tr>
<td>select#bla.com</td>
</tr>
<tr>
<td>doNotSelectToo#bla.com</td>
</tr>
<tr>
<td>someOtherEmail#bla.com</td>
</tr>
<tr>
<td>select#bla.com</td>
</tr>
</table>
You can separate the duplicate-check from the rendering of the HTML and do it beforehand, so that you know whether an email is duplicated when you are rendering your HTML.
Example
<?php
$data_array = [
[ 'Email' => 'test#example.com' ],
[ 'Email' => 'test2#example.com' ],
[ 'Email' => 'test#example.com' ],
[ 'Email' => 'test#example.com' ],
];
$email_count = [];
// Count emails.
foreach($data_array as $entry) {
$email = $entry['Email'];
if (isset($email_count[$email])) {
$email_count[$email]++;
} else {
$email_count[$email] = 1;
}
}
// Render table.
foreach($data_array as $entry) {
if($email_count[$entry['Email']] > 1){
$row_color = "#f00"; // Red
}else{
$row_color = "#fff"; // White
}
echo "<tr style='color:".$row_color.";'>";
echo "<td>".$entry['Email']."</td>";
echo "</tr>" . PHP_EOL;
}
Result
<tr style='color:#f00;'><td>test#example.com</td></tr>
<tr style='color:#fff;'><td>test2#example.com</td></tr>
<tr style='color:#f00;'><td>test#example.com</td></tr>
<tr style='color:#f00;'><td>test#example.com</td></tr>
I am having two set off arrays, first array is holding data about colors, second about dimensions.
$colors= array(
"27" => "RAL 9002",
"255" => "RAL 9006",
"341" => "RAL 8019",
"286" => "RAL 7016",
"141" => "RAL 3009",
"171" => "RAL 6028",
"121" => "RAL 8004",
"221" => "RAL 5010",
"101" => "RAL 3000",
"273" => "RAL 9007",);
$dimensions = array
(
array(0.3,1245),
array(0.35,1245),
array(0.40,1100),
array(0.45,1245),
array(0.50,1245),
array(0.60,1245),
array(0.70,1245),
);
Values above are used for queries. I wanted to make an array which will hold data, and later in comparison will print data. Query can return result NULL
foreach($colors as $key => $value)
{
//print "Boja $key . <br>";
foreach($dimensions as $data )
{
//print "Debljina $data[0], Sirina $data[1] Boja $key <br>";
$sql = "SELECT Debljina, Sirina, sum(Kolicina) as suma
FROM jos_ib_repromaterijali WHERE Debljina = '$data[0]' AND Sirina = '$data[1]' AND Boja = '$key'";
$q = $conn -> query($sql);
$vrijednosti = array();
while($r=$q->fetch()) {
$debljina = $r['Debljina'];
$sirina = $r['Sirina'];
$kolicina = $r['suma'];
$vrijednosti[] = $debljina . $sirina . $kod . $kolicina;
}
}
}
Once I get results, I create html table
<div class="col-lg-6">
<table class="table table-bordered">
<thead>
<tr>
<th><?php echo "Dimenzije"; ?> </th>
<?php foreach($colors as $boja) { ?>
<th><?php echo $boja; ?> </th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php foreach($dimensions as $dim) {
?>
<tr>
<td><?php echo $dim[0] . ' X ' . $dim[1]; ?> </td>
<td><?php
if (isset($vrijednosti[$dim[0] . $dim[1]])) {
echo "asdas";
}
else {
echo "error";
}
?> </td>
</tr>
<?php }
?>
</tbody>
</table>
Table print headers and dimensions as it should, I am having trouble in matching rows with column. If someone give me an advice it would be appreciated.
Query can give result NULL; if that is result It should print 0 below color for specific row.
I have two set off array, in one I hold colors in another dimensions. Those array I use to make queries and later print result in table.
$colors = array(
"27" => "RAL 9002",
"255" => "RAL 9006",
"341" => "RAL 8019",
"286" => "RAL 7016",
"141" => "RAL 3009",
"171" => "RAL 6028",
"121" => "RAL 8004",
"221" => "RAL 5010",
"101" => "RAL 3000",
"273" => "RAL 9007");
$dimensions = array
(
array(0.3,1245),
array(0.35,1245),
array(0.40,1100),
array(0.45,1245),
array(0.50,1245),
array(0.60,1245),
array(0.70,1245)
);
According to the values above I make query:
foreach($colors as $key => $value)
{
foreach($dimensions as $data )
{
$sql = "SELECT sum(Kolicina) as suma
FROM materijali WHERE Debljina = $data[0] AND Sirina = $data[1] AND Boja = $key";
$q = $conn->query($sql);
while($r=$q->fetch()) {
$debljina = $data[0];
$sirina = $data[1];
$kolicina = $r["suma"];
$kod = $key;
if(empty($kolicina)) {
$kolicina = '0.00';
}
$vrijednosti[] = $debljina.'x'.$sirina.'--'.$kod;
}
}
}
Result I receive from above I should print in table. For some reason I dont get good result, It always displays red. Not sure if I should use isset() function.
<div class="col-lg-6">
<table class="table table-bordered">
<thead>
<tr>
<th><?php echo "Dimenzije"; ?> </th>
<?php foreach($colors as $boja) { ?>
<th><?php echo $boja; ?> </th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php foreach($dimensions as $dim) {
?>
<tr>
<td><?php
echo $dim[0] . ' X ' . $dim[1]; ?> </td>
<?php
foreach($colors as $key => $value) {
// if ($vrijednosti[$dim[0].'--'.$dim[1].'--'.$key]) {
if (isset($vrijednosti[$dim[0].'x'.$dim[1] .'--'.$key])) {
echo '<td style="background:blue"> </td>';
}
else {
echo '<td style="background:red">' .$vrijednosti[$dim[0].'x'.$dim[1] .'-'.$key] .' </td>';
}
}
?> </td>
</tr>
<?php }
?>
</tbody>
</table>
</div>
When I dump array $vrijednosti I get following format (I just took 5elements)
array(70) {
[0]=>
string(12) "0.3x1245--27"
[1]=>
string(13) "0.35x1245--27"
[2]=>
string(12) "0.4x1100--27"
[3]=>
string(13) "0.45x1245--27"
[4]=>
string(12) "0.5x1245--27"
Table does not show good results, dont have idea what could be wrong? Any help would be appreciated.
Table looks like this. Table forms looks well. In cells it should display $kolicina which I get in while loop.
$vrijednosti[] = $debljina.'x'.$sirina.'--'.$kod;
creates a numeric indexed array. But your if checks for the presence of an array key in $vrijednosti:
if (isset($vrijednosti[$dim[0].'x'.$dim[1] .'--'.$key])) { ... }
Thats always false. You have to check for an element, not a key:
if (in_array($dim[0].'x'.$dim[1] .'--'.$key, $vrijednosti) { ... }
I am using Laravel as framework and the class simple_html_parse_DOM to parse a website .
The problem is the foreach didn't work .It just parse the first element.
I want to parse the elements in every tr :
example of the page i want to parse:
<tr>
<td class="allf">ADWYA</td>
<td>8.85</td>
<td>9.02</td>
<td>8.84</td>
<td>3256</td>
<td>29369</td>
<td><b>9.02</b></td>
<td><span class="quote_up2">0.00%</span></td>
</tr>
<tr>
<td class="allf">AETECH</td>
<td>1.19</td>
<td>1.19</td>
<td>1.19</td>
<td>1193</td>
<td>1420</td>
<td><b>1.19</b></td>
<td><span class="quote_up2">0.00%</span></td>
</tr>
<tr>
<td class="allf">AIR LIQUIDE TUNISIE</td>
<td>147.00</td>
<td>147.00</td>
<td>147.00</td>
<td>6</td>
<td>882</td>
<td><b>147.00</b></td>
<td><span class="quote_up2">0.00%</span></td>
my code:
$html = new simple_html_dom();
$html->load_file('http://www.ilboursa.com/marches/aaz.aspx');
$e = $html->find('#tabQuotes',0)->find('.alri');
$i=0;
foreach ($e as $alri) :
$Nom = $alri->children($i)->children(0)->plaintext;
$Ouverture = $alri->children($i)->children(1)->plaintext;
$Haut=$alri->children($i)->children(2)->plaintext;
$Bas=$alri->children($i)->children(3)->plaintext;
$Volumetitre =$alri->children($i)->children(4)->plaintext;
$Volumedt=$alri->children($i)->children(5)->plaintext;
$Dernier =$alri->children($i)->children(6)->plaintext;
$Variation=$alri->children($i)->children(7)->plaintext;
$cours = \App\Cours::create(array(
'Nom'=>$Nom,
'Ouverture'=>$Ouverture,
'Haut'=>$Haut,
'Bas'=>$Bas,
'Volumetitre' =>$Volumetitre,
'Volumedt'=>$Volumedt,
'Dernier'=>$Dernier,
'Variation' =>$Variation
));
$i++;
endforeach;
$e = $html->find('#tabQuotes',0)->find('.alri');
// For every table
foreach ($e as $alri)
// For every tr
foreach($alri->find('tr') as $tr) {
// array of td
$tds = $tr->find('td');
$Nom = $tds[0]->plaintext;
$Ouverture = $tds[1]->plaintext;
$Haut = $tds[2]->plaintext;
$Bas = $tds[3]->plaintext;
$Volumetitre = $tds[4]->plaintext;
$Volumedt = $tds[5]->plaintext;
$Dernier = $tds[6]->plaintext;
$Variation = $tds[7]->plaintext;
$cours = \App\Cours::create(array(
'Nom'=>$Nom,
'Ouverture'=>$Ouverture,
'Haut'=>$Haut,
'Bas'=>$Bas,
'Volumetitre' =>$Volumetitre,
'Volumedt'=>$Volumedt,
'Dernier'=>$Dernier,
'Variation' =>$Variation
));
}
The function works well for creating a formatted table from a multi-dimensional array and assigning it to a variable. When there is a sub-array it will nest another table nicely but instead, i'd like to display a comma-separated list (conditional only for sub-arrays).
What php function / logic could help me accomplish this and how should I implement it?
It currently displays this...
<table>
<tr>
<td><span style="color:#ffeeee;">Keywords</span></td>
<td>
<span style="color:#eeeeff;">
<table>
<tr>
<td><span style="color:#ccffcc;">Alex</span></td>
</tr>
<tr>
<td><span style="color:#ccffcc;">Mic</span></td>
</tr>
<tr>
<td><span style="color:#ccffcc;">snowboarding</span></td>
</tr>
</table>
</span>
</td>
</tr>
</table>
Would prefer something like this...
<table>
<tr>
<td><span style="color:#ffeeee;">Keywords</span></td>
<td><span style="color:#eeeeff;">Alex, Mic, snowboarding</span></td>
</tr>
</table>
PHP Code (display_array_processor.php):
//----------------------------------------------------//
/*
// Iterating complex php multidimensional multi-level array
// to html table using return instead of echo
*/
//----------------------------------------------------//
if($_POST)
{
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
$output = json_encode(array( //create JSON data
'type'=>'error',
'text' => 'Sorry Request must be Ajax POST'
));
die($output); //exit script outputting json data
}
$process_method = $_POST["process_method"];
$process_levels = $_POST["process_levels"];
//Sanitize input data using PHP filter_var()
//$process_method = filter_var($_POST["process_method"], FILTER_SANITIZE_STRING);
//----------------------------------------------------//
if ($process_levels == "noheaders") {
// Sample array from shell command (No Headings)...
$myArray = array(
array(
"Size" => "914 kB",
"Date" => "2015:02:08 18:01:00-08:00",
"Attributes" => "Regular; (none)",
"Names" =>
array(
"Alex Smith",
"Mike Jones"
),
"Keywords" =>
array(
2015,
"Alex",
"Mike",
"snowboarding",
array(
'A snow',
'B cold',
'C fun'
)
),
"Software" => "Invisible Space-Monkey Monitoring System 01",
"Duration" => 4800
)
);
} elseif ($process_levels == "headers") {
// Sample array from shell command (With Headings)...
$myArray = array(
array(
"SourceFile" => "test.txt",
"Tool" =>
array(
"Version" => 11.12
),
"File" =>
array(
"Size" => "104 kB",
"ModifyDate" => "2016:02:08 18:01:00-08:00"
),
"Region" =>
array(
"RegionAppliedToDimensionsUnit" => "pixel",
"RegionName" =>
array(
"Alex Smith",
"Mike Jones"
),
"Subject" =>
array(
2015,
"Alex",
"Mike",
"snowboarding"
)
)
)
);
} else {
// Small Sample Array...
$myArray = array(
"Source" => "test.txt"
);
}
if ($process_method == "tables") {
//$html = recursive_array($myArray);
$html = recursive_array_table($myArray);
} elseif ($process_method == "commas") {
$html = array_table($myArray);
} else {
$html = "Do not know the process method.";
}
//$html .= '<p><textarea rows="10" cols="75">'.$process_method.'</textarea></p>';
$output = json_encode(array('type'=>'message', 'text' => $html));
die($output);
}
//----------------------------------------------------//
// Recursive Array Functions - COMMA SUB-ARRAYS
//----------------------------------------------------//
// Two generic utility functions:
function is_nested($array) {
return is_array($array) && count($array) < count($array, COUNT_RECURSIVE);
}
function is_assoc($array) {
return is_array($array) && array_keys($array) !== range(0, count($array) - 1);
}
function array_table_row($key, $row) {
$content = array_table($row);
return "<tr>\n<th>$key</th>\n<td>$content</td>\n</tr>\n";
}
function array_table($array) {
if (is_nested($array) || is_assoc($array)) {
$content = implode("", array_map('array_table_row', array_keys($array), $array));
return "<table border=1>\n<tbody>\n$content</tbody>\n</table>\n";
}
// At this point $array is too simple for a table format:
$color = is_array($array) ? "922" : "292";
$content = htmlspecialchars(is_array($array) ? implode(", ", $array) : $array);
return "<span style='color:#$color'>$content</span>";
}
//----------------------------------------------------//
// Recursive Array Functions - TABLE SUB-ARRAYS (ORIG)
//----------------------------------------------------//
function recursive_array_table($array) {
return "<table border=1>\n<tbody>\n" .
recursive_array_table_row($array) .
//implode("", array_map('recursive_array_table_row', array_keys($array), $array)) .
"</tbody>\n</table>\n";
}
function recursive_array_table_row($array) {
foreach($array as $key => $value) {
$content .= '<tr>';
if (is_array($value)) {
$content .= '<td colspan="1"><span style="color:#992222;">'.htmlspecialchars($key).'</span></td>';
$content .= '<td><span style="color:222299;">'.recursive_array_table($value).'</span></td>';
} else {
// Filter out some info...
//if (
//($key != "Error") &&
//($key != "SourceFile")
//) {
//if (!is_numeric($key)) {
$content .= '<td>'.htmlspecialchars($key).'</td>';
//}
$content .= '<td><span style="color:#229922;">'.$value.'</span></td>';
//}
}
$content .= '</tr>';
}
return $content;
}
function recursive_array_csv($array) {
//return is_array($array)
// ? "[" . implode(", ", array_map('array_csv', $array)) . "]"
// : htmlspecialchars($array);
}
HTML:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#submit_btn").click(function() {
post_data = {
'process_levels' : jQuery('select[name=process_levels]').val(),
'process_method' : jQuery('select[name=process_method]').val(),
'process_name' : jQuery('input[name=process_name]').val(),
'process_msg' : jQuery('textarea[name=process_message]').val()
};
jQuery.post('display_array_processor.php', post_data, function(response){
if(response.type == 'error'){ //load json data from server and output message
output = '<div class="error">'+response.text+'</div>';
}else{
output = '<div class="success">Done! Here is the response:</div><div>'+response.text+'</div>';
}
jQuery("#contact_results").hide().html(output).slideDown();
}, 'json');
});
});
</script>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="form-style" id="contact_form" style="max-width:650px;">
<div id="contact_results"></div>
<br />
<div id="contact_body">
<h3>Simple Bootstrap jQuery Ajax Form</h3>
Levels:
<select name="process_levels" id="process_levels" class="input-sm form-control" />
<option value="noheaders">No Headers</option>
<option value="headers">With Headers</option>
<option value="">None</option>
</select>
Method:
<select name="process_method" id="process_method" class="input-sm form-control" />
<option value="commas">Commas</option>
<option value="tables">Tables</option>
<option value="">None</option>
</select>
<input type="submit" id="submit_btn" value="Submit" />
</div>
</div>
</body>
</html>
Here is a way to generate a table where values will be represented as comma-separated as soon as the data is considered more suitable for that.
Concretely, data will be represented as comma-separated if it is a non-nested, sequential array, so without named keys.
Here it is:
// Two generic utility functions:
function is_nested($array) {
return is_array($array) && count($array) < count($array, COUNT_RECURSIVE);
}
function is_assoc($array) {
return is_array($array) && array_keys($array) !== range(0, count($array) - 1);
}
function array_table_row($key, $row) {
$content = array_table($row);
return "<tr>\n<th>$key</th>\n<td>$content</td>\n</tr>\n";
}
function array_table($array) {
if (is_nested($array) || is_assoc($array)) {
$content = implode("", array_map('array_table_row', array_keys($array), $array));
return "<table border=1>\n<tbody>\n$content</tbody>\n</table>\n";
}
// At this point $array is too simple for a table format:
$color = is_array($array) ? "922" : "292";
$content = htmlspecialchars(is_array($array) ? implode(", ", $array) : $array);
return "<span style='color:#$color'>$content</span>";
}
See in eval.in
Output for the "headers" sample data:
Previous answer:
function array_csv($array) {
return is_array($array)
? "[" . implode(", ", array_map('array_csv', $array)) . "]"
: htmlspecialchars($array);
}
function array_table_row($index, $row) {
return "<tr>\n <th colspan='2'>Row ". ($index+1) . "</th>\n</tr>\n" .
implode("", array_map(function ($key, $value) {
return "<tr>\n <td>" . htmlspecialchars($key) . "</td>\n" .
" <td><span style='color:#" . (is_array($value) ? "ccf" : "cfc") . ";'>" .
array_csv($value) . "</span></td>\n</tr>\n";
}, array_keys($row), $row));
}
function array_table($array) {
return "<table border=1>\n<tbody>\n" .
implode("", array_map('array_table_row', array_keys($array), $array)) .
"</tbody>\n</table>\n";
}
echo array_table($myArray);
See it run on eval.in.
This is the output for the sample data:
<table border=1>
<tbody>
<tr>
<th colspan='2'>Row 1</th>
</tr>
<tr>
<td>Size</td>
<td><span style='color:#cfc;'>914 kB</span></td>
</tr>
<tr>
<td>Date</td>
<td><span style='color:#cfc;'>2015:02:08 18:01:00-08:00</span></td>
</tr>
<tr>
<td>Attributes</td>
<td><span style='color:#cfc;'>Regular; (none)</span></td>
</tr>
<tr>
<td>Names</td>
<td><span style='color:#ccf;'>[Alex Smith, Mike Jones]</span></td>
</tr>
<tr>
<td>Keywords</td>
<td><span style='color:#ccf;'>[2015, Alex, Mike, snowboarding, [A snow, B cold, C fun]]</span></td>
</tr>
<tr>
<td>Software</td>
<td><span style='color:#cfc;'>Invisible Space-Monkey Monitoring System 01</span></td>
</tr>
<tr>
<td>Duration</td>
<td><span style='color:#cfc;'>4800</span></td>
</tr>
</tbody>
</table>
It was not clear from your question what you want to do when arrays are nested deeper, going beyond the level of the comma-separated values.
With the above code, the comma-separated lists are wrapped in brackets, so it is clear where sub-arrays start and end within such lists.
Note that you should escape some characters of your data (like less-than and ampersand characters) with a function like htmlspecialchars.