Php 如何在数组中foreach数组
如何正确地使用每个数组 代码:Php 如何在数组中foreach数组,php,Php,如何正确地使用每个数组 代码: while($row=mysqli\u fetch\u assoc($result)) { 如果($use_sef_links==true) { $sef_link=“{$row['title']}({$row['home_team_name']}vs{$row['away_team_name']}){$row['dt']}”; $sef_link=str_replace(“-”,“,$sef_link”); $sef#u link=preg#u replace(
while($row=mysqli\u fetch\u assoc($result))
{
如果($use_sef_links==true)
{
$sef_link=“{$row['title']}({$row['home_team_name']}vs{$row['away_team_name']}){$row['dt']}”;
$sef_link=str_replace(“-”,“,$sef_link”);
$sef#u link=preg#u replace(“#\s{1,}#,”,“,$sef#link”);
$sef_link=str_replace(“,”-“,$sef_link”);
$sef_-link=preg_-replace(“#[^a-zA-Z0-9-]”,“,$sef_-link”);
$sef_link.=''u m'.$row['id'...html';
$row['link']=$sef_link;
}
其他的
{
$row['link']='match.php?id='。$row['id'];
}
$matches[$row['seasure\u name'][$row['game\u day']][]=$row;
}
foreach($matches as$match){?>
下赛季比赛-比赛日
- 即将到来的比赛
- 比赛开始了
- 体育场
-
VS
-
如果我只是打印($matches),那么我就可以看到所有的赛季和比赛。。
但是我不能把每一场比赛都当作一场比赛
我把图像放在这里,显示如果我打印匹配的字符串,它会显示什么
图片:
不过,不必要的嵌套太多了 顺便说一句,您只需要正确地使用foreach(
foreach数组_表达式为$key=>$value
)和数组选择器。对于内部数组,使用foreach没有任何好处,因为您只有一个索引数组_表达式为:
(foreach $matches as $matchIndex => $matchDetails){
$currentMatch = $matchDetails[0];
print_r($currentMatch);
// or whatever you want to do with that
}
我本想写这篇评论的,但是没有足够的空间 给定数组的结构,
$matches['seasure\u name']['game\u day'][
我想你想要:
foreach($matches as $seasonName => $season) {
// print the season-header
echo $seasonName;
foreach($season as $gamedayID => $gameday) {
// print the gameday header
echo $gamedayID;
foreach($gameday as $match) {
// print the details from the match
echo $match['title'];
}
}
}
这是按季节分组的版本。
查看我的注释以获得解释!查看数组的结构
$matches
()。在获得实际数据之前,您将看到有两个层次结构。这两个层次结构是“季节名称”和“比赛日”。因此,要么您以不同的方式构建数组$matches($matches[]=$row;
),或者您的访问方式不同:foreach($matches['KSJM…'][2]作为$match){…
。我建议使用第一种方式,因为您不需要层次结构(直到现在-除非您希望按季节分组。)在提供文本时,请不要使用图像。
foreach($matches as $seasonName => $season) {
// print the season-header
echo $seasonName;
foreach($season as $gamedayID => $gameday) {
// print the gameday header
echo $gamedayID;
foreach($gameday as $match) {
// print the details from the match
echo $match['title'];
}
}
}