Hey guys i had an idea yesterday. Can you help me with this. Here is the code
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;
}
}
}
}
}
The will will run and produce the correcr result but i want extra iteratinos not to run. In other words
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;
}
}
}
I know this is not possible in php or any other language but i want an alternative or may be in future they may add this to increase performance.
You could change the way the arrays are structured so it's in the form:
$subCategoriesDetail[$cat_id] = array();
$teamsDetail[$sub_cat_id] = array();
Then you would use:
foreach($categoriesDetail as $categories)
{
foreach($subCategoriesDetail[$categories->id] as $subCategories)
{
foreach($teamsDetail[$subCategories->id] as $team)
{
echo $team->name;
}
}
}
If you want do this by php then you have to wait for unrealistic time.
You can do this by sql joins. Take inner joins of tables and get result. It is possible through queries. That's why joins are made to increase performance
. You can write your code if you fails to make join of them. We will definitely try to increase performance of your system.
This would be only syntactic sugar, PHP would have to do exactly the same as you did with the if, it can't do some magic.
Some like such language features, others don't, i myself am reluctant because i think the more features a language has to do the same thing, the more difficult it is to learn the language.