Reputation: 494
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
Upvotes: 1
Views: 1829
Reputation: 3537
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.
Upvotes: 1
Reputation: 13557
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,
sounds like you're hitting the execution timeout (just as @Haroon mentioned). You might want to have a look at your error log or check if the request failed with status 500 (Internal Server Error).
set_time_limit() allows you to (re)set the execution timeout as you please. But, if your script is running more than 30 seconds (which should be the default setting for execution timeout), you might want to investigate an alternate route of building that array.
foreach($locations as $loctn){
//Request of the XML,
$result = //A large array coming from a XML SOAP respond;
sounds like you're pulling data from a webservice. It also sounds like you might be (for a large set of items in $locations
) be doing quite a number of requests on that webservice.
Accessing webservices may be slow. if you have any way of doing that outside your process ("job queue", something with cron job, …) do it. Also check if the data provided by the webservice can be cached and reused.
Upvotes: 1