草庐IT

php - 处理大型数组时如何提高php的执行速度?

coder 2024-04-21 原文

如果我的问题很愚蠢,请原谅我。但请有人告诉我或建议我如何解决此问题。实际上我有一个大的多维数组

像这样

   $array_value =  Array
            (
                [0] => Array
                    (
                        [0] => sdfsf
                        [1] => fghbfh
                        [2] => sgddfsg
                        [3] => ujmnm
                        [4] => jkluik
                        ..
                        ..
                        ..
                        ..
                        ..
                        ..
                        [150] => jhbjhbjh
                     )
                 [1] => Array
                    (
                        [0] => 44062
                        [1] => 45503
                        [2] => 44062
                        [3] => fdg
                        [4] => dfgdg
                        ..
                        ..
                        ..
                        ..
                        ..
                        ..
                        [150] => jhbjhbjh
                     )
              ....
              ....
              ....
               [590] => Array
                (
                    [0] => 44062
                    [1] => 45503
                    [2] => 44062
                    [3] => fdg
                    [4] => dfgdg
                    ..
                    ..
                    ..
                    ..
                    ..
                    ..
                    [150] => jhbjhbjh
                 )
    )

600 个数组里面有 600 个数组,我有 150 个数组值。当我使用此数组时,if foreach 条件和 for 循环条件需要超过 5 分钟才能完成执行。

我在循环中使用了两个 foreach 循环和一个 for 循环以及 4 个方法函数。执行需要超过 5 分钟。我需要更快地加载。

我不知道如何增加脚本的执行时间或如何处理这个数组。请有人建议或帮助我如何处理这个问题。谢谢你。

        foreach ($Final_Data as $line_no => $val) {
            foreach($val as $col_name=> $col_value) {

                if ($col_name === 'tid' || $col_name === 'pid' || $col_name === 'local_list_id') 
                {
                        if(empty($col_value)) {
                           $null_error[] = "empty value in ".$col_name;
                        }elseif( $col_value !== null && !is_numeric( $col_value) ) {                        
                            $where_Error[] ="non numeric value in ".$col_name." = ".$col_value;
                        }
                        $where_content[]= $col_name . "= '" . $col_value."'";

                        if($col_name =='pid'){
                            $pid = $col_value;
                        }   

                }elseif(!empty($col_name) && in_array($col_name, $update))
                {

                        if($col_name=="country"){
                                ...
                        }elseif($col_name=="state"){
                                ...
                        }elseif($col_name=="district"){
                                ....
                        }elseif($col_name=="post_category")
                        {
                        ....


                            $CategoryList=array();                          
                            if(count($locationArr) > 0 && count(locationCategory($locationArr)) > 0)
                            {
                                .....
                            }




                            if(strlen($col_value)==0 || empty($col_value))
                            {
                                    ......
                             }elseif(ValidateLength($col_name,$columnValue,$maxl) === false || VulnerableExists($columnValue)===false)
                             {
                                   if(ValidateLength($col_name, $columnValue,$maxl) === false){
                                        ....
                                    }
                                    if(VulnerableExists($columnValue)===false){
                                        ....
                                    }
                             }else
                             {
                                    ......



                                    foreach($post_cat as $Category){



                                        if(get_cat_ID($Category) == 0) {                                        
                                            if(!in_array($Category, $CategoryList)){
                                            .....
                                            }
                                        }
                                    }




                                    /*****Category Mapping Start****/
                                    if(count($post_cat)>0 && isset($pid) && !empty($pid) && count($CategoryList)==0)
                                    {
                                        $postCat = array(); 
                                        $ex_catid = array();

                                        $post_categories = get_the_category( $pid );                                

                                        foreach($post_categories as $category) {

                                            if(in_array($category->name, $post_cat)){   
                                                .....
                                            }
                                        }



                                        $array_dif = array_diff($post_cat,$postCat);




                                        foreach($array_dif as $pc){
                                                ......
                                                if($cat_Id!=0) array_push($ex_catid,$cat_Id);
                                        }
                                        if(count($array_dif)!=0){
                                            ......
                                        }

                                     }elseif(count($CategoryList)>0)
                                     {

                                     }
                            }
                        }

                            $columnName=$col_name;
                            $columnValue=mysqli_real_escape_string($link, $col_value);      



                        if(ValidateLength($col_name,$col_value,$maxl)===false)
                        {
                            ......
                        }elseif(VulnerableExists($col_value)===false)
                        {
                        ......
                        }else
                        {
                        ......
                        }           



                }                           
            }           


        die();



            //  echo implode(", " ,$where_content)."<br>";
            //  echo  implode(", " ,$update_content);

                if(!empty($val['pid']) && !empty($val['tid']) && !empty($val['local_list_id']) ) {
                    if(count($vuln_error) <= 0 && count($length_error)<=0 && count($null_error)<=0  && count($CategoryList)<=0){
                        ....

                    }else
                    {
                        ....

                    }                   
                }


                //die();
            unset($where_content);
            unset($update_content);
            unset($null_error);
            unset($vuln_error);
            unset($length_error);
            unset($CategoryList);
            echo "<br><br>";


        }

我只给出了我的代码的基本结构。这就是我的整个脚本在 forloop 和 foreach 条件中有很多 if 条件的方式。但所有条件都是强制性的,因为它是为了验证和一些操作。请有人帮我解决这个问题。谢谢

这个数组中最重要的事情和问题是值是 1000 个字符和超过 1000 个字符。这里我只给出了 4 位数字和 5 个字符串。

最佳答案

我们可以使用 array_key_exists() 代替 for 循环中的 if else,也可以使用三元运算符。

关于php - 处理大型数组时如何提高php的执行速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33500611/

有关php - 处理大型数组时如何提高php的执行速度?的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  4. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  5. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  6. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  7. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  8. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  9. ruby - 多次弹出/移动 ruby​​ 数组 - 2

    我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby​​数组,我们在StackOverflow上找到一

  10. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

随机推荐