-1

私は文テーブルを持っています:

======================================================================
|  id_row  |  id_document |   id_sentence   |       sentence         | 
======================================================================
|     1    |       1      |       0         | Example sentences A    |
|     2    |       1      |       1         | Example sentences B    |
|     3    |       2      |       0         | Example sentences C    |
|     4    |       2      |       1         | Example sentences D    |
======================================================================

=> のように同じ id_document を持つ文を配列にマージしたいArray( [1] => Array ( [0] => Example sentences A [1] => Example sentences B ) [2] => Array ( [0] => Example sentences C [1] => Example sentences D ))

コードは次のとおりです。

<?php
require_once 'conf.php';
$sql = mysql_query('SELECT sentence FROM sentence group by id_document') or die(mysql_error());
while ($row = mysql_fetch_array($sql)) {
    $sentence[] = $row['sentence'];
}
print_r($sentence);
?>

しかし、id_document ごとに 1 つの文しか得られません。助けて。ありがとうございました :)

4

4 に答える 4

3

で配列にインデックスを付け、次のid_documentように配列に新しいエントリを追加します。

while ($row = mysql_fetch_array($sql)) {
    $sentence[ $row['id_document'] ][] = $row['sentence'];
}
于 2012-07-16T01:16:35.080 に答える
1

あなたは書ける:

while ($row = mysql_fetch_array($sql))
{
   $sentence[$row['id_document']][] = $row['sentence'];
}
于 2012-07-16T01:17:13.387 に答える
1
while ($row = mysql_fetch_array($sql))
  $sentence[$row['id_document']][] = $row['sentence'];
于 2012-07-16T01:17:31.817 に答える
0

これが私の最終的なコードです:

<?php

require_once 'conf.php';
$sql = mysql_query('SELECT sentence, id_document FROM sentence ') or die(mysql_error());
while ($row = mysql_fetch_array($sql)) {
   $sentence[$row['id_document']][] = $row['sentence'];
}
print_r($sentence);

?>
于 2012-07-16T04:01:28.760 に答える