0

Following code generates an editor warning for not using $key in the code. Any idea how to avoid this warning? Is there any similar check done by the PHP parses too?

array_walk($services, function(&$value, $key) {
    $value = str_replace('xxx', '', $value);
});
nanobash
  • 5,419
  • 7
  • 38
  • 56
Obaid Maroof
  • 1,523
  • 2
  • 19
  • 40

2 Answers2

1

From the manual documentation for array_walk:

Typically, callback takes on two parameters. The array parameter's value being the first, and the key/index second.

You can simply omit the $key as it isn't being used inside the callback function.

array_walk($services, function(&$value) {
    $value = str_replace('xxx', '', $value);
});

It's important to note that what you have is perfectly valid PHP code. Just enable error reporting (if you haven't already) and fix any errors the PHP parser throws. There's no reason to change it just because your IDE complains so. In this particular case, it doesn't matter though.

Community
  • 1
  • 1
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
1

Just remove it.

array_walk($services, function(&$value) {
    $value = str_replace('xxx', '', $value);
});

But note it's an editor warning, it's not PHP warning.

xdazz
  • 158,678
  • 38
  • 247
  • 274