Alexis_D
Alexis_D

Reputation: 1958

how to find a specific objet in a liste of object (other way than a loop)

I would like to know if it is possible to easily retrieve an object from a list through its id (or any other way). So far to do so I have to do a loop and test each object to see if it is the correct one.

EXEMPLE
Here as an exemple I retrieve from my DBB a liste of object picture (this list is pictures).

step_1 => Determining the best picture id depending on the attribut scorelike. the highest scorelike picture will be the best picture)

step_2 => I want to set the attribut bestpicture to YES for the picture whose id equal the value of variable $id_of_best_picture, otherwise it should be set to NO.

    // STEP_1 - DETERMINING THE BEST PICTURE ID
    // ----------------------------------------
    $bestscorelike = O;
    foreach($pictures as $picture)
    {
      $scorelike = $picture->getScorelike();
      if($scorelike > $bestscorelike)
      {
        $bestscorelike = $scorelike;
        $id_of_bestpicture = $picture->getId();
      }
    }

    // STEP_2 - HAVING TO DO A LOOP TO FIND THIS OBJECT AGAIN
    // ------------------------------------------------------
    foreach($pictures as $picture)
    {
      if($picture->getId() == $id_of_bestpicture)
      {
        $picture->setBestpicture('YES');
      }
      else
      {
        $picture->setBestpicture('NO');
      }
    }

Upvotes: 0

Views: 49

Answers (2)

Alexis_D
Alexis_D

Reputation: 1958

I modified here the answer of Oleg (less line code, but do not know if more optimised).

the iteration

    $bestscorelike = -1; // no scorelike can be negative, so on the first loop $bestPictureObject will be defined
    foreach($pictures as $picture)
    {
      $picture->setBestpicture('NO');
      $scorelike = $picture->getScorelike();
      if($scorelike > $bestscorelike)
      {
        $bestscorelike = $scorelike;
        $bestPictureObject = $picture;
      }
    }
    $bestPictureObject->setBestpicture('YES);

About the idea of creating an array of object, the key of the array being the id of the picture:

$arrayPicture[$picture->getId()] = $picture;

Therefore I can find picture easily with this arrayPicture if I know its id.

Upvotes: 0

Oleg Samorai
Oleg Samorai

Reputation: 390

If you want set "Yes" to one of picture from pictures array, you should use code like this;

$bestscorelike = 0;
$bestPictureObject = null;
foreach($pictures as $picture)
{
    $scorelike = $picture->getScorelike();
    if($scorelike > $bestscorelike)
    {
        /**
         * Now in the $bestPictureObject exists picture with scorelike less then current $picture
         */
        if (!is_null($bestPictureObject)) {
            $bestPictureObject->setBestpicture('NO');
        }
        $bestscorelike = $picture->getScorelike();
        $bestPictureObject = $picture;
    } else {
        $picture->setBestpicture('NO');
    }
}
$bestPictureObject->setBestpicture('YES');

You can create list with keys equals $object->id and have access like $pictures[$best_picture_id].

Upvotes: 2

Related Questions