0

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.

1
  • Need to reduce iterations to increase performance. Commented Mar 22, 2012 at 12:01

3 Answers 3

1

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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

+1 for "i think the more features a language has to do the same thing, the more difficult it is to learn the language."
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.