[PHP] array_reverse() and garbage collection

2008-12-07 Thread Richard Heyes
Hi,]

So I've been thinking, because occassionally I do, about
array_reverse() and how to implement it. Of course, it's entirely
theoretical, because it would a total waste of time. But I'm wondering
which of two methods would be best.

1. Do it correctly and in place. ie, loop through the the first half
of the array once swapping element 0 with the end one, element 1 with
the next to last one and so on.

2. Loop thru the array once creating a new one. Normally this would
take up a little more memory because you end up with two copies of the
array in memory just before the end of the function. But what if you
set the original element to null once you've assigned it to the new
array? The most memory I can see it using is two copies of one
element. But only if garbage collection kicks in before you start on
the next element. Does garbage collection kick in that often? From
what I remember from an article by Derick Rethans, if the refcount is
zero, then it gets cleaned up.

Cheers.

-- 
Richard Heyes

HTML5 Graphing for FF, Chrome, Opera and Safari:
http://www.rgraph.org (Updated December 5th)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] array_reverse() and garbage collection

2008-12-07 Thread Michael Kubler
What about using array_pop to take the bottom array element from the 
first one and add it to the second?
So basically you are feeding the 2nd array from the bottom of the 1st 
one, and assuming garbage collection is done for each call of array_pop, 
then you'd only need the memory usage of one copy of the array, not two.


The downside is that it's not going to be as fast as just looping 
through, and making two copies, but the upside is less memory usage.
I can see it being particularly useful for processing large arrays on 
memory restricted webservers which would possibly not have the ram for 
two copies of the array in memory at the same time.


Not sure if that actually helps, seeing as array_reverse() is already a 
function that's probably written in C which could just make a copy with 
re-arrange the pointers or something.


Michael Kubler
*G*rey *P*hoenix *P*roductions http://www.greyphoenix.biz



Richard Heyes wrote:

Hi,

So I've been thinking, because occassionally I do, about
array_reverse() and how to implement it. Of course, it's entirely
theoretical, because it would a total waste of time. But I'm wondering
which of two methods would be best.

1. Do it correctly and in place. ie, loop through the the first half
of the array once swapping element 0 with the end one, element 1 with
the next to last one and so on.

2. Loop thru the array once creating a new one. Normally this would
take up a little more memory because you end up with two copies of the
array in memory just before the end of the function. But what if you
set the original element to null once you've assigned it to the new
array? The most memory I can see it using is two copies of one
element. But only if garbage collection kicks in before you start on
the next element. Does garbage collection kick in that often? From
what I remember from an article by Derick Rethans, if the refcount is
zero, then it gets cleaned up.

Cheers.


[PHP] array_reverse

2002-06-22 Thread Alexander Mclean III

I have problem with array_reverse.

I just want to call info from my MySQL / db / table / and display the 
info in reverse.

here is a sample of the code:

HTML
?php 
$db = mysql_connect(localhost, root, ); 
mysql_select_db(learndb,$db); 
$result = mysql_ query(SELECT * FROM personnel,$ db); 
echo TABLE BORDER=2; 
echoTRTDBFull Name/BTDBNick 
Name/BTDBOptions/B/TR; 
while($myrow = mysql_fetch_array($result)) 
{ 
echo TRTD.$myrow[firstname]. 
.$myrow[lastname].TD.$myrow[nick]; 
echo TDa href=\view.php?id=.$myrow[id].\View/a; 
} 
echo /TABLE; 
? 
/HTML

This would echo  info by the order of the id.

Help?



Alexander McLean III
Designer / Developer / Illustrator
AM3 Design
am3design.com
[EMAIL PROTECTED]



Re: [PHP] array_reverse

2002-06-22 Thread Julie Meloni

AMI This would echo  info by the order of the id.

I  would recommend doing that in your SQL, unless you're doing other
special processing.

// for ascending order
SELECT * FROM personnel ORDER BY id ASC

// for descending order, aka reverse
SELECT * FROM personnel ORDER BY id DESC


- Julie

-- Julie Meloni
-- [EMAIL PROTECTED]
-- www.thickbook.com

Find Sams Teach Yourself MySQL in 24 Hours at
http://www.amazon.com/exec/obidos/ASIN/0672323494/thickbookcom-20


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] array_reverse() recursive

2002-06-10 Thread Chris Boget

Does anyone have or know of a function that will take an array and 
do array_reverse() recursively?  I have the following array/structure:


Array
(
[0] = Array
(
[0] = Array
(
[0] = bob
)

)

[1] = Array
(
[2] = Array
(
[other] = joe
[3] = Array
(
[joe] = monster
[4] = Array
(
[monster] = vision
[4] = 
)

[5] = 
)

[4] = Array
(
[joe] = vision
[4] = 
)

[5] = 
)

[3] = 
)

[2] = Array
(
[3] = Array
(
[inside] = this
[4] = Array
(
[this] = briggs
[5] = Array
(
[briggs] = monster
[6] = Array
(
[monster] = vision
[6] = 
)

[7] = 
)

[6] = Array
(
[briggs] = this
[7] = 
)

[7] = 
)

[5] = 
)

[4] = 
)

[3] = Array
(
[4] = Array
(
[outside] = that
[4] = 
)

[5] = 
)

)

And nothing I do seems to be working.  I can't reverse any of the inner
arrays.  Anyone have any ideas?

Chris



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] array_reverse() recursive

2002-06-10 Thread Stuart Dallas

Monday, June 10, 2002, 5:07:48 PM, you wrote:

 Does anyone have or know of a function that will take an array and 
 do array_reverse() recursively?  I have the following array/structure:

Something like this (UNTESTED!!)...

function recursive_array_reverse($arr)
{
foreach ($arr as $key = $val)
{
if (is_array($val))
$arr[$key] = recursive_array_reverse($val);
}
return $arr;
}

Let me know if this works.

-- 
Stuart


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] array_reverse() recursive

2002-06-10 Thread Stuart Dallas

Monday, June 10, 2002, 5:12:50 PM, you wrote:

 Monday, June 10, 2002, 5:07:48 PM, you wrote:

 Does anyone have or know of a function that will take an array and 
 do array_reverse() recursively?  I have the following array/structure:

 Something like this (UNTESTED!!)...

 function recursive_array_reverse($arr)
 {
 foreach ($arr as $key = $val)
 {
 if (is_array($val))
 $arr[$key] = recursive_array_reverse($val);
 }
 return $arr;
 }

 Let me know if this works.

D'oh, forgot the call to array_reverse :).

function recursive_array_reverse($arr)
{
foreach ($arr as $key = $val)
{
if (is_array($val))
$arr[$key] = recursive_array_reverse($val);
}
return array_reverse($arr);
}

-- 
Stuart


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php