Ad

Need Help To Optimize Php Iteration

- 1 answer

In order for me to get the data I need to iterate though different tables to get the data I need.

I have my quires that are giving me arrays and I have to iterate through them and calculate and so an so forth

below is a sample code:

$CompanyDepartmentandUserWiseDataString = array();

            foreach($Arrays1 as $Array1)
            {
                foreach($Arrays2 as $Array2)
                {
                    if($Array2->id == $Array1->id)
                    {
                        $SelectedCompanyDepartmentPercentage = calculate y;                        

                            foreach($Arrays3 as $Array3)
                            {
                                if($Array3->id == $Array1->id)
                                {
                                  $CompanyDepartmentandUserWiseDataString[] = list the catagories;  
                                }
                            }                 

                            foreach($Arrays3 as $Array3)
                            {                            
                                if($Array3->id == $Array1->id)
                                {
                                    foreach($Arrays4 as $Array4)
                                    {                                        
                                        if($Array4->id == $Array3->id)
                                        {
                                            $SelectedCompanyDepartmentUserPercentage = calculate data;

                                            foreach($Arrays5 as $Array5)
                                            {
                                                if($Array5->id == $Array4->id)
                                                    $CompanyDepartmentandUserWiseDataString[] = print;
                                                }

                                            }

                                        }

                                    }

                                }

                            }

                    }
                }               

            }

            return $CompanyDepartmentandUserWiseDataString;

the above code is generating the following:

data = [{ y: 55.11, color: colors[0]

         }, {
             y: 21.63, color: colors[1], drilldown: {
               name: 'Firefox versions', categories: ['Firefox 3.6', 'Firefox 4.0', 'Firefox 3.5', 'Firefox 3.0', 'Firefox 2.0'], level: 1,
                                         data: [13.52, 5.43, 1.58, 0.83, 0.20], 
                      color: colors[1]
            }
         }, {
            y: 11.94, color: colors[2], drilldown: {
               name: 'Chrome versions', categories: ['Chrome 10.0', 'Chrome 11.0', 'Chrome 8.0', 'Chrome 9.0', 'Chrome 12.0', 'Chrome 6.0', 'Chrome 5.0', 'Chrome 7.0'], 
                                        data: [9.91, 0.50, 0.36, 0.32, 0.22, 0.19, 0.12, 0.12], 
                      color: colors[2]
            }
         }, {
            y: 7.15, color: colors[3], drilldown: {
               name: 'Safari versions', categories: ['Safari 5.0', 'Safari 4.0', 'Safari Win 5.0', 'Safari 4.1', 'Safari/Maxthon', 'Safari 3.1', 'Safari 41'], 
                                        data: [4.55, 1.42, 0.23, 0.21, 0.20, 0.19, 0.14], 
                      color: colors[3]
            }
         }, {
            y: 2.14, color: colors[4], drilldown: {
               name: 'Opera versions', categories: ['Opera 11.x', 'Opera 10.x', 'Opera 9.x'], 
                                       data: [1.65, 0.37, 0.12], 
               color: colors[4]
            }
         }];

How can I optimize this to speed up performance. the system is very slow

please help

Ad

Answer

You could first do some preprocessing to turn your arrays to associative arrays, where every entry is keyed by its id:

function keyArray($arr) {
    $result = [];
    foreach($arr as $element) {
        $result[$element->id] = $element;
    }
    return $result;
}

$keyedArrays2 = keyArray($Arrays2);
$keyedArrays3 = keyArray($Arrays3);
$keyedArrays4 = keyArray($Arrays4);
$keyedArrays5 = keyArray($Arrays5);

Then the job becomes a lot easier, as you will only have one loop over the first array, and will be able to find the matching elements in the other arrays without any further looping:

foreach($Arrays1 as $Array1)
{
    if (isset($keyedArrays2[$Array1->id])) {
        $Array2 = $keyedArrays2[$Array1->id];
        // make calculations that depend on $Array2
    }
    if (isset($keyedArrays3[$Array1->id])) {
        $Array3 = $keyedArrays3[$Array1->id];
        // make calculations that depend on $Array3
    }
    if (isset($keyedArrays4[$Array1->id])) {
        $Array4 = $keyedArrays4[$Array1->id];
        // make calculations that depend on $Array4
    }
    if (isset($keyedArrays5[$Array1->id])) {
        $Array5 = $keyedArrays5[$Array1->id];
        // make calculations that depend on $Array5
    }
}               
Ad
source: stackoverflow.com
Ad