输出具有相同值的数组

我有一个SQL查询结果(数组):“标题”,“内容”和“名称”这是我var_dump:


array(28) {

  [0]=>

  array(3) {

    ["title"]=>

    string(10) "Basis Task"

    ["content"]=>

    string(43) "https://www.wrike.com/open.htm?id=440908999"

    ["name"]=>

    string(14) "Christian Wahl"

  }

  [1]=>

  array(3) {

    ["title"]=>

    string(10) "Basis Task"

    ["content"]=>

    string(5) "MySQL"

    ["name"]=>

    string(14) "Christian Wahl"

  }

  [2]=>

  array(3) {

    ["title"]=>

    string(4) "Test"

    ["content"]=>

    string(3) "PHP"

    ["name"]=>

    string(14) "Christian Wahl"

  }

  [3]=>

  array(3) {

    ["title"]=>

    string(4) "Test"

    ["content"]=>

    string(3) "PHP"

    ["name"]=>

    string(14) "Christian Wahl"

  }

  [4]=>

  array(3) {

    ["title"]=>

    string(10) "Basis Task"

    ["content"]=>

    string(7) "content"

    ["name"]=>

    string(9) "Elena Ott"

  }

(我切断了数组的末尾,使其更清晰一些)这些是分配给用户的任务。


现在我想输出“名称”(面板标题)和“标题”和“内容”(面板正文)。


它应该看起来像这样,但对于每个名字:


它应该是什么样子的

我试图自己找到解决方案,但没有成功:(我希望你能帮助我?


人到中年有点甜
浏览 108回答 2
2回答

慕少森

只需循环访问您的数据并呈现它$data = [];foreach ($databaseResult as $row) {&nbsp; $data[$row['name']][] = $row;}foreach($data as $name => $stuff) {&nbsp; echo $name . '<br>';&nbsp; foreach($stuff as $row) {&nbsp; &nbsp; echo $row["title"] . ':' . $row["content"] . '<BR>';&nbsp; }}

白衣染霜花

可以使用前循环。例如:foreach($yourarrayvariable as $data){&nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; echo "<td class='heading'>".$data['name']."</td>";&nbsp; &nbsp; echo "<div class='content'>";&nbsp; &nbsp; echo "<td>".$data['title']."</td>";&nbsp; &nbsp; echo "<td>".$data['content']."</td>";&nbsp; &nbsp; echo "</div>";&nbsp; &nbsp; echo "</tr>";}并根据需要在foreach中创建所需的模板外观类。
打开App,查看更多内容
随时随地看视频慕课网APP