Re: Need routine to tell me number of dimensions in array.

Re: Need routine to tell me number of dimensions in array.

am 16.03.2010 00:43:42 von Ashley Sheridan

--=-xXlK7eoqJD74NXIQnLxy
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Mon, 2010-03-15 at 16:45 -0700, Daevid Vincent wrote:

> Anyone have a function that will return an integer of the number of
> dimensions an array has?
>
> I did some quick searches and came up with nothing.
> The closest was here of someone asking the same thing, but his solution
> isn't right:
> http://www.bigresource.com/PHP-count-array-dimensions-VrIahx 1b.html
> http://php.net/manual/en/function.count.php
>
> From a human standpoint, it's easy to see, "oh, this is a TWO
> dimensional"...
>
> Array
> (
> [0] => Array
> (
> [0] => Data marked as faulty or timestamps before 2005 or in
> the future (2035)
> [1] => bad_data
> )
>
> [1] => Array
> (
> [0] => Hardware Part Numbers
> [1] => hardware_part_numbers
> )
>
> [2] => Array
> (
> [0] => Software Part Numbers
> [1] => software_part_numbers
> )
>
> [3] => Array
> (
> [0] => Software Version Number
> [1] => software_version_numbers
> )
>
> [4] => Array
> (
> [0] => Configuration Part Numbers
> [1] => configuration_part_numbers
> )
>
> )
>
> From a programatic POV, it's not quite that easy to see "this is a THREE
> dimensional", since element [0][0] is missing and it actually starts at
> [0][1], so you can't do an is_array($foo[0][0]) on it (but if you did on
> [0][1] it would pass) so you have to itterate over ALL elements of the
> array until you find one that hits or you exhaust that dimension. But then
> you have to traverse any subdimensions too, most likely recursively.
>
> Array
> (
> [0] => Array
> (
> [1] => Array
> (
> [0] => Aircraft Registration
> [1] => aircraft_registration
> )
>
> [2] => Array
> (
> [0] => Aircraft Type-Subtype
> [1] => aircraft_type_subtype
> )
>
> [3] => Array
> (
> [0] => System
> [1] => system_type_name
> )
>
> [4] => Array
> (
> [0] => Flight Count
> [1] => flight_count
> )
>
> ...
>
>
> Then it gets even more complex as this has all sorts of holes in it...
>
> Array
> (
> [0] => Array
> (
> [0] => Array
> (
> [0] => Flight Number
> [1] => flight_number
> )
>
> [4] => Array
> (
> [0] => Timestamp Departure
> [1] => timestamp_departure
> )
>
> [6] => Array
> (
> [0] => Timestamp Arrival
> [1] => timestamp_arrival
> )
>
> [8] => Array
> (
> [0] => Departure City
> [1] => departure_city
> )
>
> [9] => Array
> (
> [0] => Arrival City
> [1] => arrival_city
> )
>
> Now I could take the time to dig in and figure this all out, but I thought
> maybe someone already had a solution they could share?
>
>


The only way to do it reliably would be to iterate the entire array,
element by element, as all the elements of an array might not
necessarily be all of the array type or int's.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-xXlK7eoqJD74NXIQnLxy--

Need routine to tell me number of dimensions in array.

am 16.03.2010 00:45:27 von Daevid Vincent

Anyone have a function that will return an integer of the number of
dimensions an array has?

I did some quick searches and came up with nothing.
The closest was here of someone asking the same thing, but his solution
isn't right:
http://www.bigresource.com/PHP-count-array-dimensions-VrIahx 1b.html
http://php.net/manual/en/function.count.php

From a human standpoint, it's easy to see, "oh, this is a TWO
dimensional"...

Array
(
[0] => Array
(
[0] => Data marked as faulty or timestamps before 2005 or in
the future (2035)
[1] => bad_data
)

[1] => Array
(
[0] => Hardware Part Numbers
[1] => hardware_part_numbers
)

[2] => Array
(
[0] => Software Part Numbers
[1] => software_part_numbers
)

[3] => Array
(
[0] => Software Version Number
[1] => software_version_numbers
)

[4] => Array
(
[0] => Configuration Part Numbers
[1] => configuration_part_numbers
)

)

From a programatic POV, it's not quite that easy to see "this is a THREE
dimensional", since element [0][0] is missing and it actually starts at
[0][1], so you can't do an is_array($foo[0][0]) on it (but if you did on
[0][1] it would pass) so you have to itterate over ALL elements of the
array until you find one that hits or you exhaust that dimension. But then
you have to traverse any subdimensions too, most likely recursively.

Array
(
[0] => Array
(
[1] => Array
(
[0] => Aircraft Registration
[1] => aircraft_registration
)

[2] => Array
(
[0] => Aircraft Type-Subtype
[1] => aircraft_type_subtype
)

[3] => Array
(
[0] => System
[1] => system_type_name
)

[4] => Array
(
[0] => Flight Count
[1] => flight_count
)

....


Then it gets even more complex as this has all sorts of holes in it...

Array
(
[0] => Array
(
[0] => Array
(
[0] => Flight Number
[1] => flight_number
)

[4] => Array
(
[0] => Timestamp Departure
[1] => timestamp_departure
)

[6] => Array
(
[0] => Timestamp Arrival
[1] => timestamp_arrival
)

[8] => Array
(
[0] => Departure City
[1] => departure_city
)

[9] => Array
(
[0] => Arrival City
[1] => arrival_city
)

Now I could take the time to dig in and figure this all out, but I thought
maybe someone already had a solution they could share?


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

RE: Need routine to tell me number of dimensions in array.

am 16.03.2010 01:20:26 von Ashley Sheridan

--=-H3CdEe4ljarGTeXsg4c9
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Mon, 2010-03-15 at 17:23 -0700, Daevid Vincent wrote:

> Oh. I know it's not a simple solution to do right Ashley. And exacerbated
> by the fact that each array dimension can have different dimensions as
> well. This is why I wanted someone else's solution first before I spend
> hours or days on one that works reliably. :)
>
>
> _____
>
> From: Ashley Sheridan [mailto:ash@ashleysheridan.co.uk]
> Sent: Monday, March 15, 2010 4:44 PM
> Subject: Re: [PHP] Need routine to tell me number of dimensions in array
>
> The only way to do it reliably would be to iterate the entire array,
> element by element, as all the elements of an array might not necessarily
> be all of the array type or int's.
>
>


Best way I can think of is to iterate the entire thing and keep a count
as you do. I'm not aware of any functions that can do what you need
there.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-H3CdEe4ljarGTeXsg4c9--

RE: Need routine to tell me number of dimensions in array.

am 16.03.2010 01:23:21 von Daevid Vincent

------=_NextPart_000_0198_01CAC464.3635F540
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

Oh. I know it's not a simple solution to do right Ashley. And exacerbated
by the fact that each array dimension can have different dimensions as
well. This is why I wanted someone else's solution first before I spend
hours or days on one that works reliably. :)


_____

From: Ashley Sheridan [mailto:ash@ashleysheridan.co.uk]
Sent: Monday, March 15, 2010 4:44 PM
Subject: Re: [PHP] Need routine to tell me number of dimensions in array

The only way to do it reliably would be to iterate the entire array,
element by element, as all the elements of an array might not necessarily
be all of the array type or int's.



------=_NextPart_000_0198_01CAC464.3635F540--

Re: Need routine to tell me number of dimensions in array.

am 16.03.2010 01:33:43 von List Manager

Daevid Vincent wrote:
> Anyone have a function that will return an integer of the number of
> dimensions an array has?
>
> I did some quick searches and came up with nothing.
> The closest was here of someone asking the same thing, but his solution
> isn't right:
> http://www.bigresource.com/PHP-count-array-dimensions-VrIahx 1b.html
> http://php.net/manual/en/function.count.php
>
> From a human standpoint, it's easy to see, "oh, this is a TWO
> dimensional"...
>

How about this... Using a slightly modified array that you posted, I came up
with this in about 10 minutes

I am working with the following data structure


$in = array(
0 => array(
0 => array('Flight Number', 'flight_number'),
1 => array(
0 => array('Timestamp Departure', 'timestamp_departure'),
1 => array('Timestamp Arrival', 'timestamp_arrival'),
)
),
1 => array('Departure City', 'departure_city'),
2 => array('Arrival City', 'arrival_city'),
);

print_r($in);

echo "\n\n";

$max_depth = 0;
$cur_depth = 0;
function max_array_depth($ar) {
global $cur_depth, $max_depth;
if ( is_array($ar) ) {
$cur_depth++;
if ( $cur_depth > $max_depth ) {
$max_depth = $cur_depth;
}
foreach ( $ar AS $row ) {
max_array_depth($row);
}
$cur_depth--;
}
}

max_array_depth($in);

echo "Max depth of array is: {$max_depth}";

?>


http://www.cmsws.com/examples/php/testscripts/daevid@daevid. com/0002.php

--
Jim Lucas
NOC Manager
541-323-9113
BendTel, Inc.
http://www.bendtel.com

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

Re: Need routine to tell me number of dimensions in array.

am 16.03.2010 03:14:04 von Robert Cummings

Jim Lucas wrote:
> Daevid Vincent wrote:
>> Anyone have a function that will return an integer of the number of
>> dimensions an array has?
>>
>> I did some quick searches and came up with nothing.
>> The closest was here of someone asking the same thing, but his solution
>> isn't right:
>> http://www.bigresource.com/PHP-count-array-dimensions-VrIahx 1b.html
>> http://php.net/manual/en/function.count.php
>>
>> From a human standpoint, it's easy to see, "oh, this is a TWO
>> dimensional"...
>>
>
> How about this... Using a slightly modified array that you posted, I came up
> with this in about 10 minutes
>
>

I am working with the following data structure
>
> >
> $in = array(
> 0 => array(
> 0 => array('Flight Number', 'flight_number'),
> 1 => array(
> 0 => array('Timestamp Departure', 'timestamp_departure'),
> 1 => array('Timestamp Arrival', 'timestamp_arrival'),
> )
> ),
> 1 => array('Departure City', 'departure_city'),
> 2 => array('Arrival City', 'arrival_city'),
> );
>
> print_r($in);
>
> echo "\n\n";
>
> $max_depth = 0;
> $cur_depth = 0;
> function max_array_depth($ar) {
> global $cur_depth, $max_depth;
> if ( is_array($ar) ) {
> $cur_depth++;
> if ( $cur_depth > $max_depth ) {
> $max_depth = $cur_depth;
> }
> foreach ( $ar AS $row ) {
> max_array_depth($row);
> }
> $cur_depth--;
> }
> }
>
> max_array_depth($in);
>
> echo "Max depth of array is: {$max_depth}";
>
> ?>

>
> http://www.cmsws.com/examples/php/testscripts/daevid@daevid. com/0002.php

Globals are dirty for this kind of recursive utility function. Here's a
cleaner example:

function get_array_depth( $array )
{
if( !is_array( $array ) )
{
return 0;
}

$maxDepth = 0;
foreach( $array as $value )
{
if( ($subDepth = get_array_depth( $value )) > $maxDepth )
{
$maxDepth = $subDepth;
}
}

return 1 + $maxDepth;
}

?>

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

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

Re: Need routine to tell me number of dimensions in array.

am 16.03.2010 14:07:49 von Richard Quadling

On 15 March 2010 23:45, Daevid Vincent wrote:
> Anyone have a function that will return an integer of the number of
> dimensions an array has?

/**
* Get the maximum depth of an array
*
* @param array &$Data A reference to the data array
* @return int The maximum number of levels in the array.
*/
function arrayGetDepth(array &$Data) {
static $CurrentDepth = 1;
static $MaxDepth = 1;

array_walk($Data, function($Value, $Key) use(&$CurrentDepth, &$MaxDepth) {
if (is_array($Value)) {
$MaxDepth = max($MaxDepth, ++$CurrentDepth);
arrayGetDepth($Value);
--$CurrentDepth;
}
});

return $MaxDepth;
}

Extending Jim and Roberts comments to this. No globals. By using a
reference to the array, large arrays are not copied (memory footprint
is smaller). And by using array_walk, a separate internal pointer is
used, so no need to worry about losing your position on the array.

Something to watch out for though is recursion in the array. If a
value in the array is a reference to another part of the array, you
are going to loop around for ever.

$Data = array(&$Data);

for example, with the line ...

echo "$CurrentDepth, $MaxDepth, $Key\n";

in the callback function() will report 17701 before crashing out (no
stack error surprisingly enough).


Regards,

Richard Quadling.


--
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

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

Re: Need routine to tell me number of dimensions in array.

am 16.03.2010 15:46:11 von Robert Cummings

Richard Quadling wrote:
> On 15 March 2010 23:45, Daevid Vincent wrote:
>> Anyone have a function that will return an integer of the number of
>> dimensions an array has?
>
> /**
> * Get the maximum depth of an array
> *
> * @param array &$Data A reference to the data array
> * @return int The maximum number of levels in the array.
> */
> function arrayGetDepth(array &$Data) {
> static $CurrentDepth = 1;
> static $MaxDepth = 1;
>
> array_walk($Data, function($Value, $Key) use(&$CurrentDepth, &$MaxDepth) {
> if (is_array($Value)) {
> $MaxDepth = max($MaxDepth, ++$CurrentDepth);
> arrayGetDepth($Value);
> --$CurrentDepth;
> }
> });
>
> return $MaxDepth;
> }
>
> Extending Jim and Roberts comments to this. No globals. By using a
> reference to the array, large arrays are not copied (memory footprint
> is smaller).

Using a reference actually increases overhead. References in PHP were
mostly useful in PHP4 when assigning objects would cause the object to
be copied. But even then, for arrays, a Copy on Write (COW) strategy was
used (and is still used) such that you don't copy any values. Try it for
yourself:


$copies = array();
$string = str_repeat( '*', 1000000 );

echo memory_get_usage()."\n";
for( $i = 0; $i < 1000; $i++ )
{
$copies[] = $string;
}
echo memory_get_usage()."\n";

?>

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

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

Re: Need routine to tell me number of dimensions in array.

am 16.03.2010 15:57:51 von Peter Lind

This is one example where references actually decrease memory usage.
The main reason is the recursive nature of the function. Try


echo memory_get_usage() . PHP_EOL;
$array =3D range(0,1000000);
$array[10] =3D range(0,10);
$array[20] =3D range(0,10);
$array[30] =3D range(0,10);
$array[40] =3D range(0,10);
$array[50] =3D range(0,10);
$array[60] =3D range(0,10);
$array[70] =3D range(0,10);
$array[80] =3D range(0,10);
$array[90] =3D range(0,10);
$array[100] =3D range(0,10);
echo memory_get_usage() . PHP_EOL;
carray($array);
function carray ($array)
{
foreach ($array as $value)
{
if (is_array($value)) carray($value);
}
echo memory_get_usage() . PHP_EOL;
echo count($array) . PHP_EOL;
}
echo memory_get_usage() . PHP_EOL;

And then compare with:


echo memory_get_usage() . PHP_EOL;
$array =3D range(0,1000000);
$array[10] =3D range(0,10);
$array[20] =3D range(0,10);
$array[30] =3D range(0,10);
$array[40] =3D range(0,10);
$array[50] =3D range(0,10);
$array[60] =3D range(0,10);
$array[70] =3D range(0,10);
$array[80] =3D range(0,10);
$array[90] =3D range(0,10);
$array[100] =3D range(0,10);
echo memory_get_usage() . PHP_EOL;
carray($array);
function carray (&$array)
{
$i =3D 0;
foreach ($array as $value)
{
if (is_array($value)) carray($value);
}
echo memory_get_usage() . PHP_EOL;
echo count($array) . PHP_EOL;
}
echo memory_get_usage() . PHP_EOL;

The memory usage spikes in the first example when you hit the second
array level - you don't see the same spike in the second example.

Regards
Peter

On 16 March 2010 15:46, Robert Cummings wrote:
>
>
> Richard Quadling wrote:
>>
>> On 15 March 2010 23:45, Daevid Vincent wrote:
>>>
>>> Anyone have a function that will return an integer of the number of
>>> dimensions an array has?
>>
>> /**
>>  * Get the maximum depth of an array
>>  *
>>  * @param array &$Data A reference to the data array
>>  * @return int The maximum number of levels in the array.
>>  */
>> function arrayGetDepth(array &$Data) {
>>        static $CurrentDepth =3D 1;
>>        static $MaxDepth =3D 1;
>>
>>        array_walk($Data, function($Value, $Key) use(=
&$CurrentDepth,
>> &$MaxDepth) {
>>                if (is_array($Val=
ue)) {
>>                     =
   $MaxDepth =3D max($MaxDepth, ++$CurrentDepth);
>>                     =
   arrayGetDepth($Value);
>>                     =
   --$CurrentDepth;
>>                }
>>        });
>>
>>        return $MaxDepth;
>> }
>>
>> Extending Jim and Roberts comments to this. No globals. By using a
>> reference to the array, large arrays are not copied (memory footprint
>> is smaller).
>
> Using a reference actually increases overhead. References in PHP were mos=
tly
> useful in PHP4 when assigning objects would cause the object to be copied=
..
> But even then, for arrays, a Copy on Write (COW) strategy was used (and i=
s
> still used) such that you don't copy any values. Try it for yourself:
>
> >
> $copies =3D array();
> $string =3D str_repeat( '*', 1000000 );
>
> echo memory_get_usage()."\n";
> for( $i =3D 0; $i < 1000; $i++ )
> {
>    $copies[] =3D $string;
> }
> echo memory_get_usage()."\n";
>
> ?>
>
> Cheers,
> Rob.
> --
> http://www.interjinn.com
> Application and Templating Framework for PHP
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



--=20

WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
Flickr: http://www.flickr.com/photos/fake51
BeWelcome: Fake51
Couchsurfing: Fake51


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

Re: Need routine to tell me number of dimensions in array.

am 16.03.2010 17:10:54 von Robert Cummings

Peter Lind wrote:
> This is one example where references actually decrease memory usage.
> The main reason is the recursive nature of the function. Try
>
> >
> echo memory_get_usage() . PHP_EOL;
> $array = range(0,1000000);
> $array[10] = range(0,10);
> $array[20] = range(0,10);
> $array[30] = range(0,10);
> $array[40] = range(0,10);
> $array[50] = range(0,10);
> $array[60] = range(0,10);
> $array[70] = range(0,10);
> $array[80] = range(0,10);
> $array[90] = range(0,10);
> $array[100] = range(0,10);
> echo memory_get_usage() . PHP_EOL;
> carray($array);
> function carray ($array)
> {
> foreach ($array as $value)
> {
> if (is_array($value)) carray($value);
> }
> echo memory_get_usage() . PHP_EOL;
> echo count($array) . PHP_EOL;
> }
> echo memory_get_usage() . PHP_EOL;
>
> And then compare with:
>
> >
> echo memory_get_usage() . PHP_EOL;
> $array = range(0,1000000);
> $array[10] = range(0,10);
> $array[20] = range(0,10);
> $array[30] = range(0,10);
> $array[40] = range(0,10);
> $array[50] = range(0,10);
> $array[60] = range(0,10);
> $array[70] = range(0,10);
> $array[80] = range(0,10);
> $array[90] = range(0,10);
> $array[100] = range(0,10);
> echo memory_get_usage() . PHP_EOL;
> carray($array);
> function carray (&$array)
> {
> $i = 0;
> foreach ($array as $value)
> {
> if (is_array($value)) carray($value);
> }
> echo memory_get_usage() . PHP_EOL;
> echo count($array) . PHP_EOL;
> }
> echo memory_get_usage() . PHP_EOL;
>
> The memory usage spikes in the first example when you hit the second
> array level - you don't see the same spike in the second example.
>
> Regards
> Peter

Doh, forgot about that :)

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

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

Re: Need routine to tell me number of dimensions in array.

am 16.03.2010 17:12:56 von Robert Cummings

Peter Lind wrote:
> This is one example where references actually decrease memory usage.
> The main reason is the recursive nature of the function. Try

BTW, it's not the recursive nature of the function causing the problem.
It's the movement of the internal pointer within the array. When it
moves the COW realizes the copy's pointer has moved and splits off the
copy. You can verify this by echoing the memory usage when you first
enter carray(). The spike occurs inside the foreach loop.

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

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

Re: Need routine to tell me number of dimensions in array.

am 16.03.2010 17:35:23 von Peter Lind

Hmm, will probably have to look inside PHP for this ... the foreach
loop will copy each element as it loops over it (without actually
copying, obviously), however there's no change happening to the
element at any point and so there's nothing to suggest to the
copy-on-write to create a new instance of the sub-array.

It should look like this:
$a = array(0, 1, 2, array(0, 1, 2, 3), 4, 5, 6, .... n);
$b = $a[3];
doStuffs($b);

Whether or not you loop over $a and thus move the internal pointer,
you don't change (well, shouldn't, anyway) $b as that's a subarray
which has it's own internal pointer, that isn't touched.

Or maybe I've gotten this completely backwards ...

Regards
Peter

On 16 March 2010 17:12, Robert Cummings wrote:
> Peter Lind wrote:
>>
>> This is one example where references actually decrease memory usage.
>> The main reason is the recursive nature of the function. Try
>
> BTW, it's not the recursive nature of the function causing the problem. It's
> the movement of the internal pointer within the array. When it moves the COW
> realizes the copy's pointer has moved and splits off the copy. You can
> verify this by echoing the memory usage when you first enter carray(). The
> spike occurs inside the foreach loop.
>
> Cheers,
> Rob.
> --
> http://www.interjinn.com
> Application and Templating Framework for PHP
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



--

WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
Flickr: http://www.flickr.com/photos/fake51
BeWelcome: Fake51
Couchsurfing: Fake51


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

Re: Need routine to tell me number of dimensions in array.

am 16.03.2010 17:43:45 von Robert Cummings

Peter Lind wrote:
> Hmm, will probably have to look inside PHP for this ... the foreach
> loop will copy each element as it loops over it (without actually
> copying, obviously), however there's no change happening to the
> element at any point and so there's nothing to suggest to the
> copy-on-write to create a new instance of the sub-array.
>
> It should look like this:
> $a = array(0, 1, 2, array(0, 1, 2, 3), 4, 5, 6, .... n);
> $b = $a[3];
> doStuffs($b);
>
> Whether or not you loop over $a and thus move the internal pointer,
> you don't change (well, shouldn't, anyway) $b as that's a subarray
> which has it's own internal pointer, that isn't touched.
>
> Or maybe I've gotten this completely backwards ...

I'm not sure of the exact reason... PHP has the following comment:

Note: Unless the array is referenced, foreach operates on a copy
of the specified array and not the array itself. foreach
has some side effects on the array pointer. Don't rely on
the array pointer during or after the foreach without
resetting it.

http://php.net/manual/en/control-structures.foreach.php

I've always found the foreach loop to a be a bit on the bizarre size,
this is probably just another one of those bizarrities :)

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

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

Re: Need routine to tell me number of dimensions in array.

am 16.03.2010 23:32:45 von Rene Veerman

maybe you should be foreach()ing with references?
php.net : search "foreach" :


As of PHP 5, you can easily modify array's elements by preceding
$value with &. This will assign reference instead of copying the
value.
$arr =3D array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value =3D $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>
This is possible only if iterated array can be referenced (i.e. is variable=
),



On Tue, Mar 16, 2010 at 5:43 PM, Robert Cummings wro=
te:
> Peter Lind wrote:
>>
>> Hmm, will probably have to look inside PHP for this ... the foreach
>> loop will copy each element as it loops over it (without actually
>> copying, obviously), however there's no change happening to the
>> element at any point and so there's nothing to suggest to the
>> copy-on-write to create a new instance of the sub-array.
>>
>> It should look like this:
>> $a =3D array(0, 1, 2, array(0, 1, 2, 3), 4, 5, 6, .... n);
>> $b =3D $a[3];
>> doStuffs($b);
>>
>> Whether or not you loop over $a and thus move the internal pointer,
>> you don't change (well, shouldn't, anyway) $b as that's a subarray
>> which has it's own internal pointer, that isn't touched.
>>
>> Or maybe I've gotten this completely backwards ...
>
> I'm not sure of the exact reason... PHP has the following comment:
>
> =A0 =A0Note: Unless the array is referenced, foreach operates on a copy
> =A0 =A0 =A0 =A0 =A0of the specified array and not the array itself. forea=
ch
> =A0 =A0 =A0 =A0 =A0has some side effects on the array pointer. Don't rely=
on
> =A0 =A0 =A0 =A0 =A0the array pointer during or after the foreach without
> =A0 =A0 =A0 =A0 =A0resetting it.
>
> =A0 =A0http://php.net/manual/en/control-structures.foreach.php
>
> I've always found the foreach loop to a be a bit on the bizarre size, thi=
s
> is probably just another one of those bizarrities :)
>
> Cheers,
> Rob.
> --
> http://www.interjinn.com
> Application and Templating Framework for PHP
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

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

Re: Need routine to tell me number of dimensions in array.

am 17.03.2010 02:10:28 von Robert Cummings

Rene Veerman wrote:
> maybe you should be foreach()ing with references?
> php.net : search "foreach" :
>
>
> As of PHP 5, you can easily modify array's elements by preceding
> $value with &. This will assign reference instead of copying the
> value.
> > $arr = array(1, 2, 3, 4);
> foreach ($arr as &$value) {
> $value = $value * 2;
> }
> // $arr is now array(2, 4, 6, 8)
> unset($value); // break the reference with the last element
> ?>
> This is possible only if iterated array can be referenced (i.e. is variable),

References in foreach don't work the way you think they work. You will
still incur the copy. At least I did when I tested earlier today :)

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

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

Re: Need routine to tell me number of dimensions in array.

am 17.03.2010 11:59:45 von Richard Quadling

On 17 March 2010 01:10, Robert Cummings wrote:
>
> Rene Veerman wrote:
>>
>> maybe you should be foreach()ing with references?
>> php.net : search "foreach" :
>>
>>
>> As of PHP 5, you can easily modify array's elements by preceding
>> $value with &. This will assign reference instead of copying the
>> value.
>> >> $arr =3D array(1, 2, 3, 4);
>> foreach ($arr as &$value) {
>>    $value =3D $value * 2;
>> }
>> // $arr is now array(2, 4, 6, 8)
>> unset($value); // break the reference with the last element
>> ?>
>> This is possible only if iterated array can be referenced (i.e. is
>> variable),
>
> References in foreach don't work the way you think they work. You will st=
ill
> incur the copy. At least I did when I tested earlier today :)
>
> Cheers,
> Rob.
> --
> http://www.interjinn.com
> Application and Templating Framework for PHP
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

The peak memory usage when using references (or not) and using
foreach() vs array_walk() was the same in all my testing.

But surprisingly, the foreach() with references all round used the
lowest memory during the looping. The worse case was mixing reference
and value passing with array_walk().

My dataset was generated by reading the C:\PHP5 directory where the
keys are the directories and saving the data as an include
(var_export()-ing it).

RIchard.
--=20
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling

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