停止循环和输出,然后继续PHP [英] Stop looping and output and then continue php

查看:106
本文介绍了停止循环和输出,然后继续PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我想现在是时候问这里我的脚本,

我有一个大阵,我想循环,然后当它涉及到一定量,然后继续循环停止,这里是脚本的示例,

  $ result_array =阵列();
$位置= //数组从XML SOAP响应来位置;的foreach($地点为$ loctn){
    //将XML的请求,
    $结果= //一个大阵从XML SOAP响应到来;    的foreach($结果为$ VAL){
        $ result_array [] ='somkey'=> $ val-> identifcator,
                          somkey1'=> $ val->语言,
                          somkey2'=> $ val-> textSubjectQualifier,
                          somkey3'=> $ val-> companyId,
                          //等等。
    }
}的print_r($ result_array);

所以第一个数组是从XML响应和地点,我再次循环它和它再次从XML和我循环再次请求到一个不同的数组,然后我要输出外循环的最后一个阵列,

不过,我在这里遇到了麻烦,因为它的一个非常大的数组,如果数组没有这么大,出来好,但是当我想让它更大然后我没有得到的结果,因为浏览器将停止它,

我试着玩的内存,但它不是解决我的问题,所以我想最好的办法是我应该停止的数组,然后输出它,然后继续循环,但我不知道该怎么做,

请帮我出这

感谢您


解决方案

 回声str_repeat('',4096); //你需要这个或类似的东西,因为大多数浏览器不显示任何信息,如果他们没有足够的解析的foreach(...){
...
    的foreach(...){
        ...
        的print_r($ result_array);
        冲洗(); //呼应缓冲区
    }
}

有时候浏览器不会直到接收到足够的数据为什么会有str_repeat()显示任何东西,那是。

从PHP手册:


  

刷新()可能无法覆盖您的网站的缓冲方案
  服务器和其对在任何客户端缓冲无影响
  浏览器。 [...]


  
  

服务器为Apache模块,比如mod_gzip,可能自己的缓冲
  这将导致flush()产生的结果不会立即被发送数据
  到客户端。


  
  

甚至浏览器也会在显示之前,缓存输入。网景,
  例如,直到它接收到一个结束的行或缓冲器文本
  开始的标签,也不会呈现表直至标签
  最外层的表所示。


  
  

Microsoft Internet Explorer的某些版本将只开始
  显示页面他们已经收到256个字节的输出后,让你
  可能需要冲洗让那些之前发送额外的空格
  浏览器显示的页面。


试图解决您的code:

 回声str_repeat('',4096);
$ result_array =阵列();
$位置= //数组从XML SOAP响应来位置;的foreach($地点为$ loctn){
    //将XML的请求,
    $结果= //一个大阵从XML SOAP响应到来;    的foreach($结果为$ VAL){
        $ result_array [] ='somkey'=> $ val-> identifcator,
                          somkey1'=> $ val->语言,
                          somkey2'=> $ val-> textSubjectQualifier,
                          somkey3'=> $ val-> companyId,
                          //等等。        的print_r($ result_array);
        冲洗();
    }
}

这code应该打印$ result_array每个循环。

Ok, I think its time to ask here for my script now,

I have a large array and I want to loop it and then stop it when it comes to a certain amount and then continue to loop, here is a sample of the script,

$result_array = array();
$locations = //array of locations coming from a XML SOAP respond;

foreach($locations as $loctn){
    //Request of the XML,
    $result = //A large array coming from a XML SOAP respond;

    foreach($result as $val){
        $result_array[] = 'somkey' => $val->identifcator,
                          'somkey1' => $val->language,
                          'somkey2' => $val->textSubjectQualifier,
                          'somkey3' => $val->companyId,
                          //etc.
    }
}

print_r($result_array);

So the first array is from a XML respond and I loop it again with the locations and its requesting again from the XML and I loop it again into a different array and then I want to output the last array outside the loop,

But I'm having trouble here because its a very large array, and if the array not so big it comes out good but when I want to make it bigger then I got no result because the browser stops it,

I tried to play around with the memory but its not solving my problem so I guess the best way would be I should stop the array and then output it and then continue looping, but I don't know how to do it,

Please help me out with it

Thank you

解决方案

echo str_repeat(' ', 4096); // you need this or something similar, because most of browsers don't print anything if they don't have enough to parse

foreach (...) {
...
    foreach (...) {
        ...
        print_r($result_array);
        flush(); // echo the buffer
    }
}

Sometimes browser won't display anything until it receives enough data, that's why there is str_repeat().

From the PHP manual:

flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser. [...]

Server modules for Apache like mod_gzip may do buffering of their own that will cause flush() to not result in data being sent immediately to the client.

Even the browser may buffer its input before displaying it. Netscape, for example, buffers text until it receives an end-of-line or the beginning of a tag, and it won't render tables until the tag of the outermost table is seen.

Some versions of Microsoft Internet Explorer will only start to display the page after they have received 256 bytes of output, so you may need to send extra whitespace before flushing to get those browsers to display the page.

Trying to fix your code:

echo str_repeat(' ', 4096);
$result_array = array();
$locations = //array of locations coming from a XML SOAP respond;

foreach($locations as $loctn){
    //Request of the XML,
    $result = //A large array coming from a XML SOAP respond;

    foreach($result as $val){
        $result_array[] = 'somkey' => $val->identifcator,
                          'somkey1' => $val->language,
                          'somkey2' => $val->textSubjectQualifier,
                          'somkey3' => $val->companyId,
                          //etc.

        print_r($result_array);
        flush();
    }
}

This code should print $result_array each loop.

这篇关于停止循环和输出,然后继续PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆