|
Posted by Csaba Gabor on 12/03/07 00:18
Osiris wrote:
> Now for the really kinky thing: running the following routine on my
> localhost(apache 2.2.3,PHP5), I get
> 2 2 2 2 2 2
> 2 1 1 1 1 1
> 2 2 1 1 1 1
> 2 2 2 1 1 1
> 2 2 2 2 1 1
> 2 2 2 2 2 1
> Running it through the Eclipse PHP Zend debugger I get (which, in my
> opinion, is right):
> 2 4 4 4 4 4
> 4 2 4 4 4 4
> 4 4 2 4 4 4
> 4 4 4 2 4 4
> 4 4 4 4 2 4
> 4 4 4 4 4 2
....
> foreach($k as $i=>& $_k)
> foreach($_k as $j=>&$__k)
> {
> $__k *= 2;
> if ($i != $j)
> $k[$j][$i ] = $__k;
> }
....
I believe there may have been some bug fixes that
affected this behaviour. Specifically, things similar to:
http://bugs.php.net/bug.php?id=29687
http://bugs.php.net/bug.php?id=35106
though I'm not sure that either of them describes your
situation. In any case, I'd expect that the top
example is an earlier PHP version that has since been
fixed.
I am getting the second set of results (with the 4's)
with PHP 5.2.4 (Aug 30, 2007), Win XP Pro. From my read
of the output, what is happening in the top example is
that the reference (&$_k or &$__k) is no longer referring
to the actual row or cell, but rather to a copy, hence it
does not get doubled on the upper triangle, after the first
pass. Since you're explicitly setting the lower triangle
values, those do reflect a doubling.
To get all 2s, except for 1s on the diagonal, you could,
in keeping with the above style, use (and this should
still produce the discrepancy between your two systems):
foreach($k as $i=>&$_k) {
foreach($_k as $j=>&$__k)
if ($i < $j) {
$__k *= 2;
$k[$j][$i ] = $__k; } }
Finally, just in case you weren't aware, you're 2nd row
starts off life as:
2 => 1, 3 => 1, 4 => 1, 5 => 1, 6 => 1
When you then insert a 2, a la $k[2][1]=$__k
then the resulting row does NOT look like:
1 => 2, 2 => 1, 3 => 1, 4 => 1, 5 => 1, 6 => 1
Instead, it looks like:
2 => 1, 3 => 1, 4 => 1, 5 => 1, 6 => 1, 1 => 2
So when you iterate that with a foreach, the
inserted entry will be last and not first.
The point being that the order numeric indeces
actually appear in PHP arrays is not always
tied to their value, unless they were entered
sequentially from 0 in the first place.
Csaba Gabor from Vienna
[Back to original message]
|