English 中文(简体)
PHP - Foreach loops and ressources
原标题:

I m using a foreach loop to process a large set of items, unfortunately it s using alot of memory. (probably because It s doing a copy of the array). Apparently there is a way to save some memory with the following code: $items = &$array;

Isn t it better to use for loops instead?

And is there a way to destroy each item as soon as they have been processed in a foreach loop.

eg.

    $items = &$array;
    foreach($items as $item)
    {
     dosomethingwithmy($item);
     destroy($item);
    }

I m just looking for the best way to process a lot of items without running out of ressources.

最佳回答

Try a for loop:

$keys = array_keys($array);
for ($i=0, $n=count($keys); $i<$n; ++$i) {
    $item = &$array[$keys[$i]];
    dosomethingwithmy($item);
    destroy($item);
}
问题回答

Resource-wise, your code will be more efficient if you use a for loop, instead of a foreach loop. Each iteration of your foreach loop will copy the current element in memory, which will take time and memory. Using for and accessing the current item with an index is a bit better and faster.

use this:

reset($array);
while(list($key_d, $val_d) = each($array)){

}

because foreach create a copy

If you are getting that large data set from a database, it can often help to try and consume the data set as soon as it comes from the database. For example from the php mysql_fetch_array documentation.

$resource = mysql_query("query");
while ($row = mysql_fetch_array($resource, MYSQL_NUM)) {
    process($row);
}

this loop will not create an in memory copy of the entire dataset (at least not redundantly). A friend of mine sped up some of her query processing by 10x using this technique (her datasets are biological so they can get quite large).





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签