yii2: BC break in ArrayHelper on 2.0.36

What steps will reproduce the problem?

$list = self::find()
    ->select(['lng', 'name' => 'name_' . Yii::$app->language])
    ->orderBy('sort, name')
    ->all();
$test = ArrayHelper::map($list, 'lng', 'name');
var_dump($test);

What is the expected result?

2.0.35 result

array(9) {
  ["Icao"]=>
  string(27) "ICAO/NATO phonetic alphabet"
  ["Lapd"]=>
  string(19) "LAPD radio alphabet"
  ["NlBe"]=>
  string(15) "Dutch (Belgium)"
  ["NlNl"]=>
  string(23) "Dutch (The Netherlands)"
  ["Fr"]=>
  string(6) "French"
  ["De"]=>
  string(6) "German"
  ["It"]=>
  string(7) "Italian"
  ["Es"]=>
  string(7) "Spanish"
  ["Useless"]=>
  string(49) "The Non-Phonetic Alphabet (Use at your own risk!)"
}

What do you get instead?

2.0.36-dev result

array(9) {
  ["Icao"]=>
  NULL
  ["Lapd"]=>
  NULL
  ["NlBe"]=>
  NULL
  ["NlNl"]=>
  NULL
  ["Fr"]=>
  NULL
  ["De"]=>
  NULL
  ["It"]=>
  NULL
  ["Es"]=>
  NULL
  ["Useless"]=>
  NULL
}

Additional info

Q A
Yii version 2.0.36-dev
PHP version 7.4.6
Operating system Debian 9

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 42 (39 by maintainers)

Most upvoted comments

How to reproduce it?

wrote to email

Looking good from my end!

With my example here, https://github.com/yiisoft/yii2/issues/18086#issuecomment-649557239

_ .35 .36
1st var_dump Causes an exception, treated as a non existent object property Result as expected
2nd var_dump Result as expected Result is always NULL (or whatever $default is) even though property exists

I could fix it with checking first with offsetExists

These smart individuals came to that conclusion based on my report and code. I have never even looked at ArrayAccess to say anything useful in this matter.

Right. I will come back with a correct test this evening then 😃