Is there any way I can avoid using array_flip to optimize performance. I am doing a select
statement from database, preparing the query and executing it and storing data as an associative array in $resultCollection
and than I have array op
and for each element in $resultCollection
am storing its outputId
in op[]
as evident from the code.
I have explained code and so my question is how can I achieve an similar alternative for array_flip with using array_flip as I want to improve performance.
$resultCollection = $statement->fetchAll(PDO::FETCH_ASSOC); $op = array(); //Looping through result collection and storing unicaOfferId into op array. foreach ($resultCollection as $output) { $op[] = $output['outputId']; } //Here op array has key as 0, 1, 2...and value as id {which I am interested in} //Flip op array to get offer ids as key $op = array_flip($op); //Doing a flip to get id as key. foreach ($ft as $Id => $Off) { $ft[$Id]['is_set'] = isset($op[$Id]); }
Advertisement
Answer
You should be able to use the key from the foreach for indexes and build the array pre-flipped like this.
foreach ($resultCollection as $key => $output) { $op[ $output['outputId'] ] = $key; }