アプリ版:「スタンプのみでお礼する」機能のリリースについて

mysqlからデータをphpで取得し以下のような多次元配列になっています。

Array
(
[0] => Array
(
[id] => 116
[name] => あああ
)

[1] => Array
(
[id] => 58
[name] => いいい
)

[2] => Array
(
[id] => 89
[name] => ううう
)

)

単純にデータを表示させたく以下のようにしましたが<th>$key2</th>の箇所が上記配列の場合
2回繰り返されて表示されてしまいます。ここはフィールド名なので1回の表示でいいのですが
どのように記述すれば思うような表示になるでしょうか?

echo "<table border=\"1\">";

echo "<tr>";

foreach ($tmp1 as $key => $val) {
foreach ($val as $key2 => $val2) {
echo "<th>" . $key2 . "</th>"; //フィールド名
}
}

echo "</tr>";

foreach ($tmp1 as $key => $val) {
echo "<tr>";
foreach ($val as $key2 => $val2) {
echo "<td>" . $val2 . "</td>"; // 取得したデータをある分だけ繰り返し
}
echo "</tr>";
}

echo "</table>";

A 回答 (3件)

表示を1回だけなら、↓では?



foreach ($tmp1 as $key => $val) {
foreach ($val as $key2 => $val2) {
echo "<th>" . $key2 . "</th>";
}
break;
}

または、

foreach ($tmp1[0] as $key => $val) {
echo "<th>" . $key . "</th>";
}
    • good
    • 0

とりあえず、前述されているもの以外の方法としまして、



//array_keysはその配列のキー(連想配列名)を全部取得する
//なので、mysqlから取得した最初の配列部分のキーを取得しておく。
$keys = array_keys($array[0]);

echo '<table border="1">';
echo '<tr>';
foreach($keys as $key){

echo '<th>'.$key.'</th>';

}
echo '</tr>';

echo '<tr>';
foreach($array as $key=>$value){
echo '<td>'.$value.'</td>';
}
echo '</tr>';

echo '</table>';

というのではいかがでしょうか。
    • good
    • 0

<?php


$arr = array(array("id"=> 116 ,"name"=>"あああ" ),array( "id"=> 58 ,"name"=>"いいい" ),array( "id"=> 89 ,"name"=>"ううう" ));

/* DOMを使っているのはソースを書きたくないっていう好みの問題。 っていうか後半まで質問内容とこれっぽっちも関係ない。*/

$doc = new DOMDocument("1.0","UTF-8");
$doctype = $doc->implementation->createDocumentType("html","-//W3C//DTD XHTML 1.0 Strict//EN","http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dt …
$doc->appendChild($doctype);

$doc->formatOutput = true;

$html = $doc->createElementNS("http://www.w3.org/1999/xhtml","html");

$head = $doc->createElementNS("http://www.w3.org/1999/xhtml","head");
$title = $doc->createElementNS("http://www.w3.org/1999/xhtml","title");
$title->appendChild($doc->createTextNode("Q5016158 TestCase 1"));

$head->appendChild($title);

$body = $doc->createElementNS("http://www.w3.org/1999/xhtml","body");

$table = $doc->createElementNS("http://www.w3.org/1999/xhtml","table");

$caption = $doc->createElementNS("http://www.w3.org/1999/xhtml","caption");
$caption->appendChild($doc->createTextNode("表"));
$table->appendChild($caption);

$thead = $doc->createElementNS("http://www.w3.org/1999/xhtml","thead");
$tr = $doc->createElementNS("http://www.w3.org/1999/xhtml","tr");

/* 関係あるのはここから。*/

/* 要は二つに分ける。*/

$tmp = $arr[0];
foreach ($tmp as $key => $val) {
$th = $doc->createElementNS("http://www.w3.org/1999/xhtml","th");
$th->appendChild($doc->createTextNode($key));
$tr->appendChild($th);
}
$thead->appendChild($tr);

$tbody = $doc->createElementNS("http://www.w3.org/1999/xhtml","tbody");
$tr = $doc->createElementNS("http://www.w3.org/1999/xhtml","tr");
foreach ($arr as $key => $val) {
$tr = $doc->createElementNS("http://www.w3.org/1999/xhtml","tr");
foreach ($val as $key2 => $val2) {
$td = $doc->createElementNS("http://www.w3.org/1999/xhtml","td");
$td->appendChild($doc->createTextNode($val2));
$tr->appendChild($td);
}
$tbody->appendChild($tr);
}
$table->appendChild($thead);
$table->appendChild($tbody);
$body->appendChild($table);
$html->appendChild($head);
$html->appendChild($body);
$doc->appendChild($html);

print($doc->saveXML());

?>
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!