6

In php 5.3 the way arrays are handled have changed.

Example array:

<?php $a = array ('foo' => 1, 'bar' => 2, 'foo' => 3); ?>

use to over write 'foo' with the last one in the array to give:

array(
    'foo' => 3,
    'bar' => 2
)

now in 5.3 it returns

array(
    'foo' => 1,
    'bar' => 2
)

I am testing on a php v5.2.11 so i can not test this my self this example is from the php.net website: http://php.net/manual/en/language.types.array.php and search the page for 5.3

would the method of setting values via

<?php
    $a['foo'] = 1;
    $a['bar'] = 2;
    $a['foo'] = 3;
?>

provide a backward compatible patch for this issue? are there any other things to watch out for when dealing with arrays in the new version of php?

2
  • 1
    This would classify as undefined behaviour. But btw, 5.3 still overwrites with the last value. Commented Dec 7, 2010 at 0:54
  • "this example is from the php.net website" — can you link to it? Commented Dec 7, 2010 at 7:50

3 Answers 3

4

This seems to identify the same, somewhat odd behavior.

http://www.tehuber.com/article.php?story=20090708165752413

Sign up to request clarification or add additional context in comments.

1 Comment

+1, shows a confirmed case where PHP 5.3 behaves differently.
2

From the manual:

Note that when two identical index are defined, the last overwrite the first.

So unless you've somehow triggered a PHP bug (unlikely), then there's something else going on that you're missing.

And to answer your question, yes, overwriting keys via the assignment operator works. However, before you start changing code around, I'd check to make sure the problem is what you currently think, because the manual does directly claim that the latter keys will overwrite the former ones.

Update: @sberry2A's link does reveal a place where PHP 5.3 is buggy (i.e., doesn't do what the manual says).

class Foo
{
  const A = 1;
  const B = 1;

  public static $bar = array(self::A => 1, self::B => 2);
}

One would expect that the value of Foo::$bar[1] is 2, but it's still 1. However, the following works properly:

class Foo
{
  const A = 1;
  const B = 1;

  public static function bar()
  {
    return array(self::A => 1, self::B => 2);
  }
}

So it's only that specific case of static property arrays indexed by different constants that have the same value. That's the only way in PHP 5.3.3 that I can trigger the behavior, but perhaps there are other ways to as well... obviously a certain behavior cannot be relied upon.

Comments

0

I think you should use unique keys for the array.

<?php $a = array ('foo' => 1, 'bar' => 2); ?>

Then update the value of foo.

<?php $a['foo'] = 3; ?>

1 Comment

Its not the point, there's is a bug as confirmed on other cases, this is not a documented change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.