我有句子表:
======================================================================
| 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中得到一个句子。帮我。谢谢:))
答案 0 :(得分:3)
在id_document
上索引数组,并向数组添加新条目,如下所示:
while ($row = mysql_fetch_array($sql)) {
$sentence[ $row['id_document'] ][] = $row['sentence'];
}
答案 1 :(得分:1)
你可以写:
while ($row = mysql_fetch_array($sql))
{
$sentence[$row['id_document']][] = $row['sentence'];
}
答案 2 :(得分:1)
while ($row = mysql_fetch_array($sql))
$sentence[$row['id_document']][] = $row['sentence'];
答案 3 :(得分: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);
?>