-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 中得到一个句子。帮我。谢谢 :)

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 回答