Reply to PHP5, foreach and memory corruption

Your name:

Reply:


Posted by Anze on 08/22/05 02:07

Hi!

I have just fixed my CMS so it works on PHP5 (it worked without problems on
PHP4 but gave weird errors on PHP5). The problem lied in this construct:

echo ("BEFORE: ".count($this->someLargeArrayWithObjects)); // outputs 11
foreach ($this->someLargeArrayWithObjects as $key=>$value)
{
$a=$value['object']->someFunction();
};
echo ("AFTER: ".count($this->someLargeArrayWithObjects)); // outputs _1_
(one)!

This is of course pseudo-code - and no, function was not changing anything.
This solved the problem:

echo ("BEFORE: ".count($this->someLargeArrayWithObjects)); // outputs 11
$keys=array_keys($this->someLargeArrayWithObjects);
foreach ($keys as $key)
{
$value=&$this->someLargeArrayWithObjects[$key];
$a=$value['object']->someFunction();
};
echo ("AFTER: ".count($this->someLargeArrayWithObjects)); // outputs 11

Has anybody else had these kind of problems? The zend.ze1_compatibility_mode
directive didn't help...
I solved it now and it might even be more effective (memory-wise), but still
- I'd like to know if it's me or something else.

Another thing: is there a nicer way to traverse an array without copying the
values? All of the foreach(), current(), next() and similar do that - but I
want to traverse an array just by referencing the values.

Regards,

Anze

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация