需要帮助来优化PHP迭代

时间:2016-01-24 10:15:15

标签: php laravel highcharts

为了让我获取数据,我需要迭代不同的表来获取我需要的数据。

我有我的quires给我数组,我必须迭代它们并计算,所以等等

下面是一个示例代码:

$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;

以上代码生成以下内容:

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]
            }
         }];

如何优化此功能以加快性能。系统很慢

请帮助

1 个答案:

答案 0 :(得分:2)

您可以先进行一些预处理,将数组转换为关联数组,其中每个条目都以其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);

然后这个工作变得容易多了,因为在第一个数组上只有一个循环,并且能够在没有任何进一步循环的情况下找到其他数组中的匹配元素:

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
    }
}