我想改变保存到db的字符串的方向。 我希望HELLO这个词看起来像这样:
H W
E O
L R
L L
O D
我试试这个,但给我看问号?:
echo "<table>";
$res=$functions->query("SELECT string1 FROM table");
while ($row = mysql_fetch_assoc($res)){
$hor=str_split($row['string1']);
foreach ($hor as $letter) {
$vert=$letter."\n";
}
echo"<th>".$vert."</th>";
}
// other functions...
echo "</table>";
答案 0 :(得分:0)
您应该使用以下代码更改您的foreach语句:
foreach($hor as $letter) {
$vert .= $letter . "\n";
}
您忘记了显示所有信件所必需的concatenating问题。
答案 1 :(得分:0)
答案 2 :(得分:0)
多田!试试这个,解释在代码中的注释中:
$str = "HELLO HOW YA DOING WORLD?";
// Convert $str to an array of rows of letters.
$strWords = explode(' ', $str);
$strLettersRowsArr = array_map('mb_split', $strWords);
// Get maximium number of rows of letters e.g. `strlen("WORLD?")` => `6`.
$maxRows = 0;
foreach ($strLettersRowsArr as $lettersArr) {
if (count($lettersArr) > $maxRows) {
$maxRows = count($lettersArr);
}
}
// Pad out the elements of $strLettersRowsArr with spaces that aren't as long as the longest word.
// e.g.
// from:
// [
// ['H', 'e', 'l', 'l', 'o'],
// ['d', 'u', 'd', 'e'],
// ]
// to:
// [
// ['H', 'e', 'l', 'l', 'o'],
// ['d', 'u', 'd', 'e', ' '],
// ]
foreach ($strLettersRowsArr as $key => &$lettersArr) {
while (count($lettersArr) < $maxRows) {
$lettersArr[] = ' ';
}
}
unset($lettersArr);
// Get the columns of letters.
// e.g.
// from:
// [
// ['H', 'e', 'l', 'l', 'o'],
// ['w', 'o', 'r', 'l', 'd'],
// ]
// to:
// [
// ['H', 'w'],
// ['e', 'o'],
// ['l', 'r'],
// ['l', 'l'],
// ['o', 'd'],
// ]
$strLettersColumnsArr = [];
for ($row = 0; $row < $maxRows; $row++) {
$strLettersColumnsArr[] = array_column($strLettersRowsArr, $row);
}
// Print out letter columns.
foreach ($strLettersColumnsArr as $lettersColumnArr) {
foreach ($lettersColumnArr as $letter) {
echo "$letter ";
}
echo "\n";
}
输出:
H H Y D W
E O A O O
L W I R
L N L
O G D
?