PHP 8.5.0 Alpha 2 available for testing

Voting

: max(six, seven)?
(Example: nine)

The Note You're Voting On

double at dumpit dot de
15 years ago
PHP: 5.2.10-2ubuntu6.3 (default apt-get installation on actual, see Date, jaunty 9.10 Ubuntu Distro - G33kWoRDs)

Have a look at your array pointer if you copy an array - the pointer will be copied, too.

For example if you got this construct:
<?php
$array
= array('zero','one','two','three','four','five','six','seven');
$array2 = $array;
next($array);
echo
key($array);
echo
key($array2);

// will output:
// 1
// 0
?>

But if you copy the array after you've setted the pointer, the pointer will be copied, too:
<?php
$array
= array('zero','one','two','three','four','five','six','seven');
next($array);
$array2 = $array;
echo
key($array);
echo
key($array2);

// will output:
// 1
// 1
?>

What's more is, that foreach not resetting the pointer after walk through:
<?php

$array
= array('zero','one','two','three','four','five','six','seven');
next($array);
$array2 = array();
foreach(
$array AS $key => $value){
echo
$key;
$array2[$key] = $value;
}
echo
var_dump(key($array));
echo
key($array2);

// will output for foreach:
// 0 1 2 3 4 5 6 7
// and for the keys
// NULL
// 0
?>

The php-functions seems to reset the pointer on the given position after walk through (i don't know the internal handling - there could be used a copy of the array, too):
<?php

$array
= array('zero','one','two','three','four','five','six','seven');
next($array);
$array2 = array_values($array);
echo
key($array);
echo
key($array2);

// will output:
// 1
// 0
?>

There are a lot Methods like array_merge($array) that will neither reset the pointer of $array nor copy the pointer to $array2. Have a look on this.
I Hope this was a little helpfull.

<< Back to user notes page

To Top