gpt4 book ai didi

php - 在php中的foreach循环中使用where条件

转载 作者:行者123 更新时间:2023-11-29 08:56:36 25 4
gpt4 key购买 nike

嘿伙计们,我昨天有一个想法。你能帮我解决这个问题吗?这是代码

foreach($categoriesDetail as $categories)
{
foreach($subCategoriesDetail as $subCategories)
{
if($categories->id == $subCategories->cat_id)
{
foreach($teamsDetail as $team)
{
if($subCategories->id == $team->sub_cat_id)
{
echo $team->name;
}
}
}
}
}

该意志将运行并产生正确的结果,但我希望不要运行额外的迭代。换句话说

foreach($categoriesDetail as $categories)
{
foreach($subCategoriesDetail as $subCategories where $categories->id == $subCategories->cat_id)
{
foreach($teamsDetail as $team where $subCategories->id == $team->sub_cat_id)
{
echo $team->name;
}
}
}

我知道这在 php 或任何其他语言中都是不可能的,但我想要一个替代方案,或者将来他们可能会添加它以提高性能。

最佳答案

您可以更改数组的结构方式,使其采用以下形式:

$subCategoriesDetail[$cat_id] = array();

$teamsDetail[$sub_cat_id] = array();

然后你会使用:

foreach($categoriesDetail as $categories)
{
foreach($subCategoriesDetail[$categories->id] as $subCategories)
{
foreach($teamsDetail[$subCategories->id] as $team)
{
echo $team->name;
}
}
}

关于php - 在php中的foreach循环中使用where条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9817188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com