eggyal我沒有回答我的問題。但這是我知道在stackoverflow重新發布代碼作爲我原來的問題後續行爲的唯一方法。
無論如何,我試過你的鏈接。 (即Transposing multidimensional arrays in PHP)但是,它不適用於我的情況。
你可以自己試試看。我附加了兩個需要試用的功能。你只需要一個mysql $ dbh連接就可以實現這個功能。你會看到,在我的功能中,我接觸到了在該鏈接中投票次數達到24次的flipdiagonally
函數。
當你調用與$ direction_v_or_h功能被設定爲h,
它的工作原理。但這對我們來說並不是新聞。這是我追求的v
模式。
與極限嘗試像這樣
SQL_getview($dbh, "select * from yourTable limit 2","h");
和
SQL_getview($dbh, "select * from yourTable limit 2","v");
,我得到的是這樣的錯誤;重複每一個領域中的表
Warning: Invalid argument supplied for foreach() in D:\Hosting\5291100\html\blueprint\sql.php on line 739
function SQL_getview($dbh,$sql,$direction_v_or_h = 'h')
{
$result = $result = mysql_query($sql,$dbh);
$fields_num = mysql_num_fields($result);
if ($direction_v_or_h == "h"):
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
while($row = mysql_fetch_row($result))
{
echo "<tr>";
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
echo "</table>";
else:
if (1): //turn this to 0 to see the good old print_r workaround
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
$row = flipDiagonally($row);
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
echo "</table>";
else:
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<pre>";
print_r ($row);
echo "</pre>";
echo "<hr>";
}
endif;
endif;
mysql_free_result($result);
}
function flipDiagonally($arr) {
$out = array();
foreach ($arr as $key => $subarr) {
foreach ($subarr as $subkey => $subvalue) {
$out[$subkey][$key] = $subvalue;
}
}
return $out;
}
見http://stackoverflow.com/a/3423692/623041 – eggyal
哈,那是相當時髦! – Scuzzy