【发布时间】:2021-10-09 04:42:00
【问题描述】:
我使用以下方法在表格中创建了列标题:
if (count($round) > 0): ?>
<table>
<thead>
<tr>
<?php foreach ($round as $colhead): array_map('htmlentities', $colhead); ?>
<th><?php echo implode('</th><th>',$colhead); ?></th>
</tr>
<?php endforeach; endif;
?>
</thead>
$round 是:
Array
(
[0] => Array
(
[32] => -7
[31] => -6
[29] => -5
[27] => -4
[23] => -3
[19] => -2
[17] => -1
[0] => 1
[2] => 2
[10] => 3
[14] => 4
[16] => 5
[33] => 6
)
)
这正是我想要的结果,因为 $round 不会总是产生相同数量的列。
我想要在这些标题下方的表格行中的数据只需要包含列标题名称的那些项目。
假设列标题是:
| -7 | -6 | -5 | -4 | -3 | -2 | -1 | 1 | 2 | 3 | 4 | 5 | 6 |
基于上面的foreach 循环。
我使用以下内容在下一行填充我的第一列:
<tbody>
<tr>
<td>
<?php foreach ($matches as $match) {
$firstCharacter = $match['match']['scores_csv'][0];
$lastCharacter = $match['match']['scores_csv'][-1];
$id1 = $match['match']['player1_id'];
$id2 = $match['match']['player2_id'];
if ($match['match']['round'] === -7)
echo "Table: ".$match['match']['player1_votes']."</br>".$participants[$id1]["name"].$participants[$id1]['misc'].": ".$firstCharacter. "</br>".$participants[$id2]["name"].$participants[$id2]['misc'].":".$lastCharacter. "</br>";
}
?>
</td>
</tr>
</tbody>
</table>
虽然它有效,但这只是因为我知道表中第一列的标题是 -7。我试图弄清楚如何仅根据列标题显示与我的列标题相同的那些项目以显示在下一行中,以便我可以执行另一个 foreach 循环来显示我的表格的其余部分。所以基本上,我需要做什么,而不是上面的 -7?我完全画了一个空白。
$match['match']['round'] 等于列标题。下面是一个小样本 $matches 以防万一。
[{"match":{"id":246811883,"tournament_id":10128202,"state":"complete","player1_id":152239163,"player2_id":152239166,"player1_prereq_match_id":null,"player2_prereq_match_id":null,"player1_is_prereq_match_loser":false,"player2_is_prereq_match_loser":false,"winner_id":152239166,"loser_id":152239163,"started_at":"2021-08-07T01: 59: 51.967+02: 00","created_at":"2021-08-07T01: 59: 51.692+02: 00","updated_at":"2021-08-07T02: 22: 29.417+02: 00","identifier":"A","has_attachment":false,"round":1,"player1_votes":1,"player2_votes":null,"group_id":null,"attachment_count":null,"scheduled_time":null,"location":null,"underway_at":"2021-08-07T02: 05: 31.834+02: 00","optional":false,"rushb_id":null,"completed_at":"2021-08-07T02: 22: 29.455+02: 00","suggested_play_order":null,"forfeited":null,"open_graph_image_file_name":null,"open_graph_image_content_type":null,"open_graph_image_file_size":null,"prerequisite_match_ids_csv":"","scores_csv":"0-2"}},{"match":{"id":246811884,"tournament_id":10128202,"state":"complete","player1_id":152239594,"player2_id":152239168,"player1_prereq_match_id":null,"player2_prereq_match_id":null,"player1_is_prereq_match_loser":false,"player2_is_prereq_match_loser":false,"winner_id":152239594,"loser_id":152239168,"started_at":"2021-08-07T01: 59: 51.984+02: 00","created_at":"2021-08-07T01: 59: 51.698+02: 00","updated_at":"2021-08-07T02: 32: 30.655+02: 00","identifier":"B","has_attachment":false,"round":1,"player1_votes":2,"player2_votes":null,"group_id":null,"attachment_count":null,"scheduled_time":null,"location":null,"underway_at":"2021-08-07T02: 05: 23.667+02: 00","optional":false,"rushb_id":null,"completed_at":"2021-08-07T02: 32: 30.690+02: 00","suggested_play_order":null,"forfeited":null,"open_graph_image_file_name":null,"open_graph_image_content_type":null,"open_graph_image_file_size":null,"prerequisite_match_ids_csv":"","scores_csv":"2-1"}}]
我尝试使用以下方法重新索引数组:
$matchesa = array_column($matches, 'match');
$matchesa = array_column($matchesa, null, 'round');
foreach ( $matches as $match ) {
$id1 = $match['match']['round'];
{
echo $matchesa[$id1]['round'].PHP_EOL;
}
}
【问题讨论】:
-
你需要根据
$round数组重构$matches数组。使用$round值作为键并从$matches获取数据。使其成为一个新数组。这只会给你$round值的数据,然后不需要比较。直接去foreach就行了 -
另外,如果你可以控制从数据库中获取数据(通过它创建
$matches),那么你可以只在那里进行索引。它会让事情变得更简单。 -
@AnantKumarSingh 我试图索引 $matches,但现在我被困在那里。还有什么想法吗?
-
你能用 json_encode($matches) 代替 var_dump 或 print_r 给我们你的匹配吗
-
我已经进行了编辑
标签: php html arrays foreach html-table