Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9079633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:52:50+00:00 2026-06-16T19:52:50+00:00

Suppose that whe have some object, derived from these class instances class A {

  • 0

Suppose that whe have some object, derived from these class instances

class A
{
...
 private b //list of object of class B
}

class B
{
...
 private c //list of object of class C
}

class C
{
...
 private id
}

Now, somewhere in my code, I’ve got this situation

function findId(array $idList)
{
 [...]
}

Where I have to find (for each element of $idList) if an element is contained into this object “cascade”

First solution

//object initialization
foreach($a->getB() as $b)
{
 foreach($b->getC() as $c)
 {
  foreach($idList as $id)
  {
   if($id == $c->getId())
   {
    //do something an break the cycle
   }
  }
 }
}

Second Solution

//object initialization

$idSet = array();
foreach($a->getB() as $b)
{
 foreach($b->getC() as $c)
 {
  $idSet[] = $c->getId();
 }
}
$idSet = array_unique($idSet);

foreach($idList as $id)
{
 if(array_search($id,$idSet) !== false)
 {
  [...]
 }
}

Which is better? There are some alternative methods for reach my goal?

IMPORTANT

There isn’t better data representation. This because these objects are some database object (doctrine2)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:52:51+00:00Added an answer on June 16, 2026 at 7:52 pm

    You can actually combine the two approaches…

    foreach($a->getB() as $b)
    {
     foreach($b->getC() as $c)
     {
        if (in_array($id, $idList)) {
         ...
    

    And if you absolutely can’t optimize in a way you won’t have inner loop, set the values of $idList as keys, and values as true, and use isset() instead of in_array(), since it’s the fastest

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that i have this HTML from a source (scrapping it) : <tr class=calendar_row
Suppose that I have the following python base class: class BaseClass(object): def a(): This
Suppose that you have a function f: List a -> a such that f
Suppose that you have a bunch of projects in your Eclipse workspace. Some are
Suppose that you have a 2D grid of cells, some of which are filled
Suppose that I have a table like: class Ticker(Entity): ticker = Field(String(7)) tsdata =
Suppose that I have class C . I can write o = C() to
Suppose that we have the following base and derived classes: #include <string> #include <iostream>
Suppose that class A inherits from class B which in turn inherits from class
Suppose that I have a list of business primary key numbers and a java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.